Create, read, update, delete (CRUD) in MongoDB learning Note 5 MongoDB

Source: Internet
Author: User
Tags findone

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

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.