This is the 22nd day of my participation in Gwen Challenge

MongoDB provides rich query functions, including combination of conditions, reverse query, query result filtering, sorting, etc. With the help of the diversified query methods of MongoDB, all kinds of query requirements of services can be fulfilled.

The find method

The basic format of the query is db.collection.find({condition}), WHERE conditions are optional, similar to WHERE conditions in MySQL. The following is an example:

// Find all documents
db.collection.find({});
// Find the document for the specified condition
db.collection.find({key: value});
// Find the user with the nickname Island code nonDb.users. find({nickname: 'booty '});Copy the code

Condition combination

You can use $and, $OR, and $NOT to set combination conditions.

// AND query format
db.collection.find({
  $and: [
    {key1: value1}, {key2: value2}
  ]
});

// OR query format
db.collection.find({
  $or: [
    {key1: value1}, {key2: value2}
  ]
});

// NOT Query formatDb.collection. find({key: {$not: {conditional expression}});Copy the code

For example, the following Users dataset:

[{nickname: 'booty ', score:90, gender: 'male'}, {nickname: 'Mary ', score:98, gender: 'female'}, 
  {nickname: 'Tom', score: 76, gender: 'male'}
]
Copy the code

Call = ‘nickname’ and ‘male’

The db. The users. The find ({$and: [{nickname: farmers' island of '}, {gender: 'male'}]});Copy the code

Find users with scores of 90 or 76:

db.users.find({
  $or: [
    {score: 90}, {score: 76}}]);Copy the code

Find users whose gender is not male and users whose search score is higher than 80:

db.users.find({
  gender: {$not: {$eq: 'male'}}
});

db.users.find({
  score: {$not: {$lt: 80}}});Copy the code

$eq = equal; $lt = less than Use conditions can be nested, such as the and and and and and and or nested, said (a | | b) && (c | | d), the format is as follows:

// AND AND OR are nested query formats
db.collection.find({
  $and: [
    {$or: [{key1: value1}, {key2: value2}]},
    {$or: [{key3: value1}, {key4: value2}]},
  ]
});
// Find the user whose nickename is island code or Mary and whose score is 90 or 76Db. Users. Find ({$and: [{$or: [{nickname: farmers' island of '}, {nickname: 'Mary'}]}, {$or: [{score:90}, {score: 76}}}]]);// find user 'nickname' and 'nickname' and 'score' 90 or 'nickname' and 'score' 76Db. Users. Find ({$or: [{$and: [{nickname: farmers' island of '}, {score:90}, {$and: [{nickname: 'booty '}, {score:76}}}]]);// nickname is the user whose score is not less than 80Db. Users. Find ({$and: [{nickname: farmers' island of '}, {score: {$not: {$lt:80}}}});Copy the code

Comparison operator

MongoDB provides the following comparison operators in the format {key: {$op: value}} :

  • $eq: The equality operator, i.ea == b;
  • $gt: Greater than, that isa > b;
  • $gte: Is greater than or equal toa >= b;
  • $lt: less than, that isa < b;
  • $lte: Is less than or equal toa <= b.

IN the query

The format of the IN query is similar to that of the comparison operator, except that the corresponding values are arrays, i.e

db.collection.find({key: {$in: [...] }});Copy the code

For example, we need to find users with scores of 90,76:

db.users.find({score: {$in: [76.90]}});
Copy the code

An in query can also be combined with other conditions, such as an and query:

db.users.find({
 $and: [
   {score: {$in: [76.90]}},
   {gender: 'male'}
   ]
});
Copy the code

Limit the number of results returned and skip data

The find method will find all the data matching conditions. Therefore, when the data set is very large, the speed will be slow and a large amount of disk I/O will be generated. If it is determined that there is only one data item, findOne can be used. Limit is returned after a specified number of results are queried, while SKIP is skipped after a specified number of results.

// Find n items of the document with the specified condition
db.collection.find({key: value}).limit(n);
// Example: Query three users with more than 80 points
db.users.find({score: {$gt: 80}}).limit(3);
// Skip the first n rows of data
db.collection.find({key: value}).skip(n);
// Example: Query users with more than 80 points and skip the first three
db.users.find({score: {$gt: 80}}).skip(3);
Copy the code

The sorting

Sort using the following format:

Db. Collection. The find (} {conditions). Sort ({key:1});
Copy the code

Where 1 represents ascending order and -1 represents descending order. For example, we need to sort in descending order by score:

db.users.find().sort({score: - 1});
Copy the code

conclusion

This article describes MongoDB’s conditional query operation, limiting the number of returns and sorting. It can be seen that although the syntax of MongoDB operations is different from SQL, they all have corresponding functions to assist query, which is also very convenient for query.