MongoDB Query Document

Source: Internet
Author: User
Tags mongodb mongodb find mongodb query mongodb tutorial

The MongoDB query document uses the Find () method. The Find () method displays all documents in an unstructured manner.

Grammar

Db.collection.find (query, projection)

query: Optional, use the query operator to specify the query criteria;

projection: Optional, use the projection operator to specify the returned key. Returns all the key values in the document when queried, just omit the parameter.

If you need to read the data in an easy-to-read way, you can use the pretty () method, which has the following syntax:

>db.col.find (). Pretty ()

Pretty () method to display all documents in a formatted manner.

Example

> Db.col.find (). Pretty ()
{
        "_id": ObjectId ("56063f17ade2f21f36b03133"),
        "title": "MongoDB Tutorial",
        "description": "MongoDB is a Nosql database",
        "by": "Rookie Tutorial",
        "url": "Http://www.runoob.com",
        "tags": [
                "Mong ODB ",
                " database ",
                " NoSQL "
        ],
        " likes ":
In addition to the find () method, there is a FindOne () method that returns only one document


MongoDB versus RDBMS Where statement comparison



MongoDB and Condition

The MongoDB find () method can pass in multiple keys (key), each separated by commas, which is the and condition of the regular SQL.

The syntax format is as follows:

>db.col.find ({key1:value1, key2:value2}). Pretty ()
Example
> Db.col.find ({"By": "Rookie Tutorial", "title": "MongoDB Tutorial"}). Pretty ()
{
        "_id": ObjectId ("56063f17ade2f21f36b03133 "),
        " title ":" MongoDB Tutorial ",
        " description ":" MongoDB is a Nosql database ",
        " by ":" Rookie Tutorial ",
        " url ":" Http://www.ru Noob.com ",
        " tags ": [
                " MongoDB ",
                " database ",
                " NoSQL "
        ],
        " likes ":

MongoDB OR Condition

>db.col.find (
   {
      $or: [
         {key1:value1}, {key2:value2}
      ]
   }
). Pretty ()

Example

>db.col.find ({$or: [{"By": "Rookie Tutorial"},{"title": "MongoDB Tutorial"}]}). Pretty ()
{
        "_id": ObjectId (" 56063f17ade2f21f36b03133 "),
        " title ":" MongoDB Tutorial ",
        " description ":" MongoDB is a Nosql database ",
        " by ":" Rookie Tutorial "," c5/> "url": "Http://www.runoob.com",
        "tags": [
                "MongoDB",
                "database",
                "NoSQL"
        ],
        "likes ":
>

combined with and or

>db.col.find ({"likes": {$gt: $}, $or: [{"By": "Rookie Tutorial"},{"title": "MongoDB Tutorial"}]}). Pretty ()
{
        "_id": ObjectId ("56063f17ade2f21f36b03133"),
        "title": "MongoDB Tutorial",
        "description": "MongoDB is a Nosql database",
        "By": "Rookie Tutorial",
        "url": "Http://www.runoob.com",
        "tags": [
                "MongoDB",
                "database",
                "NoSQL"
        ],
        "likes":
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.