MongoDB's query method

  • 2020-06-12 10:54:46
  • OfStack

Code:


db.blogs.insert([
  {
    "author": " zhang 3",
    "title": "MongoDB Introduction to the ",
    "content": " It is between relational and non-relational databases 1 Kind of NoSQL Database, with C++ Write, 1 It is agile, scalable and extensible 1 Body's high-performance, document-oriented general-purpose database ",
    "tags": [
      "MongoDB",
      "NoSQL"
    ],
    "comment": [
      {
        "name": "Jack",
        "detail": "Good ! ",
        "date": ISODate("2015-07-09 09:55:49")
      },
      {
        "name": "Tom",
        "detail": "Hello World ! ",
        "date": ISODate("2015-07-09 18:12:35")
      },
      {
        "name": "Alice",
        "detail": " hello Mongo ! ",
        "date": ISODate("2015-07-10 20:30:30")
      }
    ],
    "readCount": 154
  },
  {
    "author": " li 4",
    "title": "1+1 How much is ",
    "content": " Some people say 1+1=2 Because this is the teacher told us from childhood; And some people say 1+1=11 That's two 1 The combination of; But some people think that 1+1=1 They feel that 1 Teams plus another 1 Teams will form 1 A stronger team! ",
    "tags": [
      "story",
      "rule",
      " mathematics "
    ],
    "comment": [
      {
        "name": " Xiao-guang wang ",
        "detail": " Every man has his own answer. ",
        "date": ISODate("2015-07-10 11:45:57")
      }
    ],
    "readCount": 367
  },
  {
    "author": " li 4",
    "title": " How to write 1 A good blog? ",
    "content": "1 And the goal; 2 , insist on; 3 And share; 4 And learning; 5 And improve ",
    "tags": null,
    "comment": [
      {
        "name": " Xiao Ming ",
        "detail": "ComeOn!!!!",
        "date": ISODate("2015-07-10 14:49:06")
      },
      {
        "name": "Nike",
        "detail": " Lifelong learning! ",
        "date": ISODate("2015-07-11 10:22:36")
      },
      {
        "name": " The little red ",
        "detail": " Insist on it, ",
        "date": ISODate("2015-07-12 12:12:12")
      }
    ],
    "readCount": 1489,
    "isTop": true
  }
])

Test Data

Based on the above test data, conduct the following basic query operation:

1. Query all blogs


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

 Note: the query 1 A document: db.blogs.findOne()

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


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

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

3. Query the blog whose author is "Zhang 3" (= operation)


db.blogs.find({"author":" zhang 3"})
 or 
db.blogs.find({"author":{"$eq":" zhang 3"}})

4, query in addition to the author for "Zhang 3" blog (! =)


db.blogs.find({"author":{"$ne":" zhang 3"}})

5. Query the blog whose author is "Li 4" and whose blog title is "Introduction to MongoDB" (Operation of and)


db.blogs.find({"author":" zhang 3","title":"MongoDB Introduction to the "})

6. Query blogs with more than 200 reads and less than 1000 reads ( > =)


db.blogs.find({"readCount":{"$gte":200,"$lt":1000}})

Note: "$lt", "$lte", "$gt", "$gte" are corresponding respectively < . < =, > . > = operation

7. Query blogs written by "Zhang 3" or "Li 4" (or operation)


db.blogs.find({"$or":[{"author":" zhang 3"},{"author":" li 4"}]})

8, query the blog tag contains "NoSQL" or "math" blog (in operation)


db.blogs.find({"tags":{"$in":["NoSQL"," mathematics "]}})

Note: not in operation


db.blogs.find({"tags":{"$nin":["NoSQL"," mathematics "]}}) // Does not contain 

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


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

 Note: the query 1 A document: db.blogs.findOne()
0

Note: null matches not only documents with a key of null, but also documents that do not contain this key

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


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

 Note: the query 1 A document: db.blogs.findOne()
1

Note: MongoDB accepts anything that conforms to regular regular expressions
An array of operating

11. The query TAB contains both "story" and "rule" blogs


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

 Note: the query 1 A document: db.blogs.findOne()
2

12. Query the first TAB for "MongoDB" blog


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

Note: Array indices start at 0

13. Query the blog with 3 tags


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

 Note: the query 1 A document: db.blogs.findOne()
4

The embedded document

14. Check out the blogs reviewed by "jack"


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

 Note: the query 1 A document: db.blogs.findOne()
5

Note: URL et al cannot be used because of the embedded document ". "problem

15. Assume 2 blogs per page, in reverse order according to the amount of reading, and take the data on page 2


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

 Note: the query 1 A document: db.blogs.findOne()
6

Note: skip(), limit(), sort(), respectively denote the number of documents skipped, the number of matches, and the sort (1 denotes positive order, -1 denotes reverse order)


Related articles: