Getting started with the crud of MongoDB

Source: Internet
Author: User
Tags findone

I. Preparation

1. Find (I like to call it a service port. If it appears below, it is called a service port.), Double-click to start;

2. Restart (I call it an operation port. If it appears below, it is an operation port.)

3. md data \ dB (Create a database named DB under the Data Directory), use dB (use this database)

II,Start

1. Create

Mongo uses the keyword insert to create a file and add it to a container. For example:

We want to save a blog post. First, we create a local variable post with the key "title", "content", and
"Date", enter the following code in the operation port and press ENTER

> Post = {"title": "My blog post ",
... "Content": "Here's my blog post .",
... "Date": new date ()}
{
"Title": "My blog post ",
"Content": "Here's my blog post .",
"Date": "sat Dec 12 2009 11:23:21 GMT-0500 (EST )"
}

Enter

> DB. Blog. insert (post)

2. Read

Use the keyword find or findone

Enter

> DB. Blog. Find () or db. Blog. findone () (Note: Please clarify the differences between them)

The result of searching for files in the container blog is as follows:

{
"_ Id": objectid ("4b23c3ca7525f35f94b60a2d "),
"Title": "My blog post ",
"Content": "Here's my blog post .",
"Date": "sat Dec 12 2009 11:23:21 GMT-0500 (EST )"
}

3. Update

To update a file, we must have two parameters. First, we must know which file to update and then what file to update. Suppose we want to add an array for storing comments to the blog post. First, we need to modify the post variable. we add a key to the post variable and enter it in the operation port.

> Post. Comments = []

Then execute the update statement.

> DB. Blog. Update ({Title: "My blog post"}, post)

Execute the find statement to see the changes.

> DB. Blog. Find ()

4. Delete

Enter

> DB. Blog. Remove ({Title: "My blog post "})

Delete the post we just put in the container.

For more information, see:Http://docs.mongodb.org/manual/core/read-operations/#queries-in-mongodb

Please point out any errors. Thank you!

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.