First, insert the document
Insert a document into a collection using the Insert () or save () method
>db. Collection_name. Insert (document)
Detailed usage can refer to MongoDB Novice Tutorial
Second, find the document
Find () Displays all documents in the collection in an unstructured way
>db. Collection_name.find ()
To display all documents in a formatted manner
>db.col.find (). Pretty ()
Here, by the way, the conditional operator:
- (>) Greater than-$GT
- (<) less than-$lt
- (>=) greater than or equal to-$gte
- (<=) less than or equal to-$lte
This allows you to query the specified document by setting criteria, such as
})
That is, all documents with a value greater than 100 for the query field "likes"
For detailed usage, please refer to MongoDB Novice Tutorial
Third, update the document
MongoDB uses the update () and Save () methods to update the documents in the collection.
Update operation
Syntax format
Db.collection. Update ( <query>, <update>, { <boolean>, <boolean> , <document> })
Parameter description:
- Query: The search condition for update, similar to where in SQL update query.
- Update: Update object and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
- upsert : Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert.
- multi : Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.
- writeconcern : Optional, throws an exception level.
Worth talking about is the update of the whole update and the local update, where the local update, MONGODB provides two modifiers, $inc and $set, as the name implies, $inc is to add the corresponding value of the field, and $set set the field directly to the corresponding value, for example
Db. user. Update ({"Name": "S"},{$inc: {"Age":5}) db. user. Update ({"Name": "S"},{$set: {"Age":5})
The age of users named "S" was increased by 5 and changed to 5 respectively.
Detailed usage Reference MONGODB rookie tutorial
Iv. Deleting documents
Syntax format
db.collection.remove ( <query>, { <boolean > , <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.
If remove () takes no parameters, all data is deleted.
Detailed usage Reference MONGODB rookie tutorial
MongoDB Document BASIC Operations