MongoDB command-line Operations

Source: Internet
Author: User

1. Connect MongoDB:

Mongodb://username:[email Protected]/dbname

2. Create a database:

Use dbname: Create the database if the database does not exist, or switch to the specified database.

3. View all databases:

Show DBS

Note:

The command does not display the newly created empty database, and if you want to show that you need to insert some data into an empty database.

The default database in MongoDB is test, and if no new database is created, the collection will be stored in test .

4. View the current database name:

Db

5. Delete the database:

Db.dropdatabase (): Deletes the current database.

6. Insert the document:

Using The use command, select the specified database and then perform the insert operation:

Db.collection_name.insert: Inserts a document into a collection of databases if the collection is not

In the database, MongoDB automatically creates the set and inserts the document.

Examples of Use:

>db.col.insert ({title: ' MongoDB command line Operation ',

Description: ' use Shell command line to manipulate MongoDB ',

By: ' Oh Joon fill ',

likes:100

})

(You can also define a document as a variable and then insert the variable)

7. View the inserted document:

Using The use command, select the specified database and then perform the query operation:

Db.collection_name.find (): Displaying documents in an unstructured manner

Db.collection_name.find (). Pretty (): Displaying documents in a structured manner

Db.collection_name.findOne (): Returns only one document (displayed in a structured manner)

Example of a conditional query:

Db.collection_name.find ({' name ': ' Wujuntian '})

Db.col.find ({"likes": {$ne:). Pretty ()// not equal to

Db.col.find ({$or: [{"By": " Oh Joon fill "},{"title": "MongoDB command operation "}]}). Pretty ()// or

Db.col.find ({"likes": {$gt: $}, $or: [{"By": " Oh Joon fill "},{"title": "MongoDB command operation "}]}). Pretty ()

Db.col.find ({likes: {$lt: $, $GT: 100}})

8. Update the Documentation:

(1) Update () method: Used to update a document that already exists.

Db.collection.update (

<query>,

<update>,

{

Upsert: <boolean>

Multi: <boolean>

Writeconcern: <document>

}

)

Parameter description:

the query:update query condition, similar to what is behind the where in the SQL update query .

Update:update objects and some updated operators (such as $, $inc ... ) and so on, can also be understood as SQL Update query Inside Set the content behind.

Upsert: Optional, this parameter means that if there is no record of update , if a new record is inserted , true is Insert, the default is False, not inserted.

Multi: optional,mongodb defaults to false, updating only the first record found, if this parameter is true, You can find out all the records on a per-condition basis.

Writeconcern: Optional, throws an exception level.

Examples of Use:

>db.col.update ({' title ': ' MongoDB '},{$set: {' title ': ' MongoDB command operation '}},{multi:true})

(2) Save Method: Replaces an existing document with an incoming document.

Db.collection.save (

<document>,

{

Writeconcern: <document>

}

)

Parameter description:

Document: documentation data.

Writeconcern: Optional, throws an exception level.

Example:

>db.col.save ({

"_id": ObjectId ("56064f89ade2f21f36b03136"),// replace the document specified by this _id

"title": "MongoDB",

"description": "MongoDB is a Nosql database ",

"Tags": [

"MongoDB",

"NoSQL"

],

"Likes": 110

})

Note:

If you do not specify _id, theSave () method is similar to the inser () method.

9. Delete the document:

Db.collection.remove (

<query>,

{

Justone: <boolean>

Writeconcern: <document>

}

)

Parameter description:

Query:(optional) The condition of the deleted document.

Justone: (optional) If set to true or 1, only one document is deleted.

Writeconcern:(optional) the level at which the exception is thrown.

Example:

>db.col.remove ({' title ': ' MongoDB '})

>db.col.remove ()// Delete all data

Ten. MongoDB $type operator

The $type operator is based on the BSON type to retrieve the matching data type in the collection and returns the result.

Example:db.col.find ({"title": {$type: 2}})

(Gets the data in the "Col" collection that is the title of the String data type)

One. Limit (): reads a specified number of data records.

Db. Collection_name.find (). Limit (number)

Examples of Use:

Db.col.find ({},{"title": 1,_id:0}). Limit (2)

Skip (): Skips a specified number of data records.

Db. Collection_name.find (). Limit (number). Skip (number)

Examples of Use:

Db.col.find ({},{"title": 1,_id:0}). Limit (1). Skip (1)

Sort (): Sorts the data records.

Sort () allows you to specify the sort fields by parameter and use 1 and -1 to specify how the sort is sorted, where 1 is in ascending order and -1 is used in descending order. If no sorting is specified, the document is sorted by default in ascending order.

Db. Collection_name.find (). Sort ({key:1})

Examples of Use:

Db.col.find ({},{"title": 1,_id:0}). Sort ({"Likes":-1})

Ensureindex (): Create an index.

Db. Collection_name.ensureindex ({key:1}) (1 means index is created in ascendingorder-1 means descending)

Example:

Db.col.ensureIndex ({"title": 1, "description":-1})

Note:

MongoDB the indexes in are stored in the B in the tree structure.

Aggregate (): aggregation.

Db. Collection_name.aggregate (aggregate_operation)

Example:

Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$sum: 1}}])

Similar to select By_user, COUNT (*) from MyCol GROUP by By_user

MongoDB command-line Operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.