A summary of the basic operation methods of checking and deleting documents in MongoDB _MONGODB

Source: Internet
Author: User
Tags mongodb mongodb collection mongodb find mongodb save mongodb tutorial mongodb update

Insert Document: Insert () method

To insert data into the MongoDB collection, you need to use the MongoDB Insert () or Save () method.

Grammar:

The basic syntax for the Insert () command is as follows:

 >db. Collection_name.insert (document)

Example:

 >db.mycol.insert({    _id: ObjectId(7df78ad8902c),    title: 'MongoDB Overview',     description: 'MongoDB is no sql database',    by: 'tutorials point',    url: 'http://www.jb51.net',    tags: ['mongodb', 'database', 'NoSQL'],    likes: 100 })
Here MyCol is the name of the collection, as created in the previous tutorial. If the collection does not exist in the database, then MongoDB creates the collection and inserts it into the document.

Insert the document, if we do not specify the _id parameter, and then mongodb this document to assign a unique objectid.

_ID is a 12-byte hexadecimal number, each document in the unique collection. 12 bytes are divided as follows:

 _id:objectid (4 bytes timestamp, 3 bytes machine ID, 2 bytes process ID, 3 bytes incrementer)

To insert multiple documents for a single query, you can pass a file of an array insert () command.

Example:

 >db.post.insert ([{    title: ' MongoDB Overview ',     Description: ' MongoDB is no SQL database ',    by: ' Tutorials point ',    url: ' http://www.jb51.net ',     Tags: [' mongodb ', ' database ', ' NoSQL '],    likes:100}, {    title: ' NoSQL database ',     description: ' NoSQL database doesn ' t have tables ',    by: ' Tutorials point ',    url: ' http://www.jb51.net ',    Tags: [' mongodb ', ' database ', ' NoSQL '],    likes:20,     comments: [         {          User: ' User1 ',           message: ' My comment ',          datecreated: New Date (2013,11,10,2,35),          LIKE:0&Nbsp;      }   ]})
To insert a file, you can also use Db.post.save (document). If you do not specify _id in the document, then work with the Save () method and the Insert () method. If _id is specified, it replaces the entire data file, which contains the _id specified save () method.


Delete Document: Remove () method

The MongoDB Remove () method is used to delete a document from the collection. The Remove () method accepts two parameters. The first is to delete the criteria, and the second is the JUSTONE flag:

(1) Deletion criteria: (optional) Delete criteria, according to the file will be deleted.

(2) Justone: (optional) If set to TRUE or 1, then only one file is deleted.

Grammar:

The basic Syntax Remove () method is as follows

 >db. Collection_name.remove (Delletion_critteria)

Example:

Consider the following data MyCol collection.

 { "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"}
The following example will delete all the files whose title is ' MongoDB Overview '

 >db.mycol.remove({'title':'MongoDB Overview'}) >db.mycol.find() { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"} >
Delete Only one.

If there are multiple records and only the first record to delete, set the Justone parameter in the Remove () method

 >db. Collection_name.remove (deletion_criteria,1)

Delete all Files:

If you do not specify a delete condition, then MongoDB deletes the entire file from the collection. This corresponds to the SQL TRUNCATE command.

 >db.mycol.remove () >db.mycol.find () >


Query Documents:
1.find () method
to query the collection data from the MongoDB, you need to use the MongoDB find () method.

Grammar:

The basic find () method syntax is as follows

 >db. Collection_name.find ()

The Find () method displays all files in an unstructured manner.

2.pretty () method

The results are shown in a format that can be used with the pretty () method.

Grammar:

 >db.mycol.find (). Pretty ()

Example:

 >db.mycol.find().pretty() {    "_id": ObjectId(7df78ad8902c),    "title": "MongoDB Overview",     "description": "MongoDB is no sql database",    "by": "tutorials point",    "url": "http://www.jb51.net",    "tags": ["mongodb", "database", "NoSQL"],    "likes": "100" } >
In addition to the find () method, there is a FindOne () method that returns a file.

RDBMS WHERE clause and MongoDB equivalent statement.

To query some of the conditions of a file, you can use the following actions

Operation Grammar Example RDBMS equivalent
Equality Key Db.mycol.find ({' By ': ' Tutorials Point '}). Pretty () where by = ' tutorials point '
Less Than {<key>:{$lt: <value>}} Db.mycol.find ({"likes": {$lt:}}). Pretty () Where likes < 50
Less Than Equals {<key>:{$lte: <value>}} Db.mycol.find ({"likes": {$lte:}}). Pretty () where likes <= 50
Greater Than {<key>:{$gt: <value>}} Db.mycol.find ({"likes": {$gt:}}). Pretty () where likes > 50
Greater Than Equals {<key>:{$gte: <value>}} Db.mycol.find ({"likes": {$gte:}}). Pretty () where likes >= 50
Not Equals {<key>:{$ne: <value>}} Db.mycol.find ({"likes": {$ne:}}). Pretty () where likes!= 50

3.AND usage in MongoDB

Grammar:

In the Find () method, if separated by multiple keys ', ', then MongoDB processes the and condition. And the basic syntax is as follows:

 >db.mycol.find ({key1:value1, key2:value2}). Pretty ()

Example

The examples given below will show all tutorials, titled "MongoDB Overview".

 >db.mycol.find({"by":"tutorials point","title": "MongoDB Overview"}).pretty() {    "_id": ObjectId(7df78ad8902c),    "title": "MongoDB Overview",     "description": "MongoDB is no sql database",    "by": "yiibai",    "url": "http://www.jb51.net",    "tags": ["mongodb", "database", "NoSQL"],    "likes": "100" } >
The example given above is equivalent to the WHERE clause ' where by= ' Yiibai ' and title= ' MongoDB Overview ', which can be in the Find clause by any number of key-value pairs.

4.MongoDB or

Grammar:

If you want to query the file based on the or condition, you need to use the $or keyword. The OR basic syntax looks like this:

 >db.mycol.find (  {    $or: [    {key1:value1}, {key2:value2}    ]  }). Pretty ()

Example

The examples given below will show all tutorials, written by ' Yiibai ' or titled ' MongoDB Overview '

 >db.mycol.find({$or:[{"by":"yiibai"},{"title": "MongoDB Overview"}]}).pretty() {    "_id": ObjectId(7df78ad8902c),    "title": "MongoDB Overview",     "description": "MongoDB is no sql database",    "by": "yiibai",    "url": "http://www.jb51.net",    "tags": ["mongodb", "database", "NoSQL"],    "likes": "100" } >
5.AND used with OR

Example

The examples given below will show that there is a file greater than 100, titled "MongoDB Overview" or ' Yiibai '. is equivalent to the SQL WHERE clause is

' Where likes>10 and (by = ' Yiibai ' OR title = ' MongoDB Overview ') '

 >db.mycol.find("likes": {$gt:10}, $or: [{"by": "yiibai"}, {"title": "MongoDB Overview"}] }).pretty() {    "_id": ObjectId(7df78ad8902c),    "title": "MongoDB Overview",     "description": "MongoDB is no sql database",    "by": "yiibai",    "url": "http://www.jb51.net",    "tags": ["mongodb", "database", "NoSQL"],    "likes": "100" } >

Update document
the MongoDB update () and Save () methods are used to update the collection of documents. The update () method updates the existing document value and replaces the Save () method in the file passed by the existing document.

1.MongoDB Update () method

The update () method updates the existing document value.

Grammar:

The basic syntax for the update () method is as follows

 >db. Collection_name.update (Selectioin_criteria, Updated_data)

Example

Consider the following data MyCol collection.

 { "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
The following example sets the file for the new title ' MongoDB Overview ', updating its title "New MongoDB Tutorial"

 >db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}}) >db.mycol.find() { "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"} >
MongoDB defaults will only update a single file to update multiple you need to set the parameter ' multi ' to True

>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
2.MongoDB Save () method

The Save () method replaces the existing document and passes the new document Save () method

Grammar

The basic syntax for the MongoDB Save () method is as follows:

 >db. Collection_name.save ({_id:objectid (), new_data})

Example

The following example will replace the file with _id as ' 5983548781331adf45ec7 '

 >db.mycol.save(    {       "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai New Topic", "by":"Yiibai"    } ) >db.mycol.find() { "_id" : ObjectId(5983548781331adf45ec5), "title":"Yiibai New Topic", "by":"Yiibai"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"} >


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.