"MongoDB Learning Note 14" MongoDB Query: Find basics

Source: Internet
Author: User
Tags mongodb query

MongoDB uses find to query, returning a subset of the documents in a collection;

Returns all documents in the Document collection blog:

> Db.post.find () {"_id": ObjectId ("54a530c3ff0df3732bac1681"), "id": 2, "name": "Joe", "age": +, "sex": 1, "SC Hool ":" Marry "} {" _id ": ObjectId (" 54a530c3ff0df3732bac1680 ")," id ": 1," name ":" Joe "," age ": +," comments ": [ "Test2", "Test9", "Test5"], "sex": 1, "school": "Marry"} >

Returns the specified document:

> Db.post.find ({"id": 1}) {"_id": ObjectId ("54a530c3ff0df3732bac1680"), "id": 1, "name": "Joe", "Age": "comme NTS ": [" Test2 "," Test9 "," Test5 "]," sex ": 1," school ":" Marry "} >

Returns the specified key value:

> db.post.find ({},{"id": 1, "Age": 1}) {' _id ': ObjectId ("54a530c3ff0df3732bac1681"), "id": 2, "age": $ {"_id" : ObjectId ("54a530c3ff0df3732bac1680"), "id": 1, "Age": >

The default is always to return the "_id" key, and the following method can not return "_id" key:

> db.post.find ({},{"id": 1, "Age": 1, "_id": 0}) {"id": 2, "age": +} {"id": 1, "Age": +} >


You can combine multiple criteria, such as querying a document with the name "Joe" and an ID of 1:

> db.post.find ({"Name": "Joe"})    {  "_id"  : objectid (" 54a530c3ff0df3732bac1681 "), " id " : 2, " name " : " Joe ", " Age " : 30,   "Sex"  : 1,  "school"  :  "Marry"  }    {  "_id"  :  objectid ("54a530c3ff0df3732bac1680"),  "id"  : 1,  "name"  :  "Joe",  "age"  : 30,  "Comments"  : [  "Test2",  "Test9",  "test5"  ],  "Sex"  : 1,  "School"  :  "Marry"  }    > db.post.find ({"Name" : "Joe", "id": 1})     {  "_id"  : objectid ("54a530c3ff0df3732bac1680"),  "id " : 1, " name " : " Joe ", " age " : 30, " comments " : [  "Test2",  "Test9",  "test5"  ],  "Sex"  : 1,  "school"  :  "Marry"  }     >



This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1599149

"MongoDB Learning Note 14" MongoDB Query: Find basics

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.