MongoDB Study Notes-curd operations

Source: Internet
Author: User

1. Create a database:

> Use tutorialSwitched to DB tutorial <br/>

Here, you may have questions about how to use the tutorial database we have not created?

In fact, databases and collections are only created when document data is inserted for the first time.

Here we use the users set.

2. Insert a record:

> DB. Users. insert ({Username: "Smith "})
3. view records:

> DB. Users. Find ()

4. Insert the second record:

> DB. Users. Save ({Username: "Jones "})
5. view the number of records:

> DB. Users. Count ()

6. query all records:

> DB. Users. Find ()

7. Conditional query:

> DB. Users. Find ({Username: "Jones "})

8. Data Update (Smith's country is Canada)

> DB. Users. Update ({Username: "Smith" },{$ set: {country: "Canada "}})9. view the updated data:

> DB. Users. Find ({Username: "Smith "})

10. Remove attributes:

> DB. Users. Update ({Username: "Smith" },{$ unset: {country: 1 }})


11. Insert complex data:

> DB. users. update ({Username: "Smith" },</P> <p >{$ set: {favotites :{</P> <p> cities: ["Chicago ", "Cheyenne"], </P> <p> movies: ["Casablanca", "The Sting"] </P> <p >}< br/> })

Here we also update the Jones record.

> DB. users. update ({Username: "Jones" },</P> <p >{$ set: {favorites: </P> <p >{</P> <p> movies: ["Casablanca ", "Rocky"] </P> <p >}< br/>}) <br/>
12. query the internal object value:

> DB. Users. Find ({"favorites. Movies": "Casablanca "})
13. To add elements to the list, you can use $ push $ addtoset, where $ addtoset cannot be repeated, the third parameter can be ignored now, and the fourth Parameter
True indicates multiple updates:

> DB. users. update ({"favorites. movies ":" Casablanca "},</P> <p >{$ addtoset: {" favorites. movies ":" The Maltese Falcon "}}, false, true)

14. data deletion:

> DB. Users. Remove ({"favorites. Cities": "Cheyenne "})
15. delete a set:

> DB. Users. Drop ()


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.