The database uses the Create read (find) update (UPDATE) Delete (remove), which is also used by MongoDB;
First, create
Add the document to the collection with the Insert function. For example
Create a database blog, add the document to the collection post (the document is put into a Post's variable first)
> post={"title": "My blog post", "context": "Here's My blog post", "date": New Date ()} > Use blog switched to DB Blo G > Db.post.insert (POST); Writeresult ({"ninserted": 1})
Second, read
Use the Find method or the FindOne method to view the documents in the collection, such as
> Db.post.find () {"_id": ObjectId ("54a50253e287e09898eab58b"), "title": "My blog post", "context": "Here ' s My blo G Post "," Date ": Isodate (" 2015-01-01t08:15:14.121z ")} > Db.post.findOne () {" _id ": ObjectId (" 54a502 53e287e09898eab58b ")," title ":" My blog post "," context ":" Here's My blog post "," date " : Isodate ("2015-01-01t08:15:14.121z")} >
Third, update
Re-assigning a variable post to a value
> Use blog switched to DB blog > post=db.post.findone () {"_id": ObjectId ("54a50253e287e09898eab 58b ")," title ":" My blog post "," context ":" Here's My blog post "," Date ": Isodate (" 201 5-01-01t08:15:14.121z ")}
Add a comments document to the variable post
> post.comments = [] []
Update Method Updates Collection
> db.post.update ({"title": "My blog post"},post); Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > Db.post.findOne () {"_id": Object Id ("54a50253e287e09898eab58b"), "title": "My blog post", "context": "Here ' s My blog post", "Date": Isodate ("2015-01-01t08:15:14.121z"), "comments": []}
Iv. deletion
Delete a document using the removed method
> Db.post.remove ({"title": "My blog post"}); Writeresult ({"nremoved": 1}) > Db.post.findOne (); Null >
Post collection is empty after deletion;
This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1598289
Create, read, update, delete (CRUD) in MongoDB learning Note 5 MongoDB