Iv. MongoDB Query

Source: Internet
Author: User
Tags mongodb query

I. The download, installation and deployment of MongoDB

Ii. Introduction to MongoDB's basic knowledge

Iii. creation, updating and deletion of MongoDB

1 Db.blogs.insert ([2     {3         "author":"Zhang San",4         "title":"about MongoDB",5         "content":"It is a NoSQL database between relational database and non-relational database, written in C + +, is a high performance document-oriented universal database with agility, scalability and extensibility.",6         "Tags": [7             "MongoDB",8             "NoSQL"9         ],Ten         "Comment": [ One             { A                 "name":"Jack", -                 "Detail":"good! ", -                 "Date": Isodate ("2015-07-09 09:55:49") the             }, -             { -                 "name":"Tom", -                 "Detail":"Hello world! ", +                 "Date": Isodate ("2015-07-09 18:12:35") -             }, +             { A                 "name":"Alice", at                 "Detail":"Hello, mongo!. ", -                 "Date": Isodate ("2015-07-10 20:30:30") -             } -         ], -         "Readcount":154 -     }, in     { -         "author":"John Doe", to         "title":"equal to a few", +         "content":"Some people say 1+1=2, because this is the teacher told us from childhood, and some people say 1+1=11, this is two 1 of the combination; But some people think 1+1=1, they feel that 1 teams plus another team, will form a more powerful team! ", -         "Tags": [ the             " Story", *             "Rule", $             "Mathematics"Panax Notoginseng         ], -         "Comment": [ the             { +                 "name":"Wang", A                 "Detail":"every man has his own answer in his heart. ", the                 "Date": Isodate ("2015-07-10 11:45:57") +             } -         ], $         "Readcount":367 $     }, -     { -         "author":"John Doe", the         "title":"How to write a good blog? ", -         "content":"1, the goal, 2, adhere to, 3, share, 4, study, 5, improve",Wuyi         "Tags":NULL, the         "Comment": [ -             { Wu                 "name":"Xiao Ming", -                 "Detail":"comeon!!!!", About                 "Date": Isodate ("2015-07-10 14:49:06") $             }, -             { -                 "name":"Nike", -                 "Detail":"Lifelong Learning! ", A                 "Date": Isodate ("2015-07-11 10:22:36") +             }, the             { -                 "name":"Little Red", $                 "Detail":"you insist on it,", the                 "Date": Isodate ("2015-07-12 12:12:12") the             } the         ], the         "Readcount":1489, -         "Istop":true in     } the])
Test Data

Based on the above test data, the following basic query operations are performed:

1. Check All Blogs

Db.blogs.find () or Db.blogs.find ({})

Note: Query one document: Db.blogs.findOne ()

2. Query the title and content of all blogs (specify the key values that need to be returned)

Db.blogs.find ({},{"title":1, "content":1, "_id":0})

Note: 1 means return, 0 means no return. By default, the "_id" key is always returned, even if the key is not specified

3, the query author for "Zhang San" blog (= operation)

Db.blogs.find ({"Author": "Zhang San"}) or Db.blogs.find ({"Author": {"$eq": "Zhang San"}})

4. Query except for "Zhang San" blog (! = action)

Db.blogs.find ({"Author": {"$ne": "Zhang San"}})

5, the query author is "John Doe" and blog titled "MongoDB Introduction" blog (and Operation)

Db.blogs.find ({"Author": "Zhang San", "title": "About MongoDB"})

6, the query reading volume is greater than or equal to 200 and less than 1000 of the blog (>= operation)

Db.blogs.find ({"Readcount": {"$gte":$, "$lt":+}})

Note: "$lt", "$lte", "$gt", "$gte" correspond to <,<=,>,>= actions respectively

7, the query author for "Zhang San" or "John Doe" blog (or operation)

Db.blogs.find ({"$or":[{"Author": "Zhang San"},{"author": "John Doe"}]})

8. Query blog tag contains "NoSQL" or "Math" blog (in Action)

Db.blogs.find ({"tags": {"$in":["NoSQL", "math"]}}) Note: not In Operation Db.blogs.find ({"tags": {"$nin":["NoSQL", "math"]}})//not included

9. Query the blog with empty tag (null operation)

Db.blogs.find ({"tags":null}) Note: Null matches not only a document with a key that is null, but also a document that does not contain this key

10, the query content contains the number "1" blog

Db.blogs.find ({"Content":/1/})

Note: It can be accepted by MongoDB as long as it conforms to regular regular expressions.

Array manipulation

11, the query tag contains both "story", but also contains the "rule" of the blog

Db.blogs.find ({"tags": {"$All":["story", "rule"]}})

12, query the first tag is "MongoDB" blog

Db.blogs.find ({"tags.  0":" MongoDB "})

Note: The subscript of the array is starting from 0

13, query the number of Labels 3 blog

Db.blogs.find ({"tags": {"$size":3}})

Inline document
14. Query "Jack" commented on the blog

Db.blogs.find ({"Comment.name": "Jack"})

Note: Because the inline document "." The problem, so you can't use URLs and so on

15, assume that each page 2 blog, according to the number of readings in reverse, take the second page of data

Db.blogs.find ({}). Skip (2). Limit (2). Sort ({"Readcount":-1})
Note: Skip (), limit (), sort (), indicating the number of skipped documents, number of matches, and sort (1 for positive order, 1 for reverse)

Iv. MongoDB Query

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.