Mongodb revisits the road (2)

Time:2021-7-22

Insert document

Using mongodb queryfind()method.

db.collection.find(query, projection)

*Query is optional. Use the query operator to specify the query conditions
*Projection is optional. The projection operator is used to specify the returned key
 

//If you want to read it in an easy way, use ` pretty ()`
db.col.find().pretty()


//In addition to find (), there is findone (). As the name suggests, only one is found

AND

Db.col.find ({"title": "ZJJ", "sex": "male"})

OR

db.col.find({$or[{key: val1}, {key1: val2}]})

Or and

db.col.find({"like": {$gt: 50}, $or:[{"by":"zjj"},{"title":"zjjasdada"}]})

Conditional Operator

1、 Query on projection

1.1 comparison operator
$eq
// =
//Query age = 20
db.person.find({ age: { $eq: 20 } })
//Equivalent to
db.person.find({age: 20})
$gt
// >
//Query age > 20
db.person.find({age: {$gt: 20}})
$lt
// <
//Query age < 20
db.person.find({age: {$lt: 20}})
$gte
// >=
//Query age > = 20
db.person.find({age: {$gte: 20}})
$lte
// <=
//Query age < = 20
db.person.find({age: {$lte: 20}})
$ne
// ≠
//Query age ≠ 20
db.person.find({age: {$ne: 20}})
$in
// ≠
//Matches any value in the array
db.person.find({qyt: {$in :[5, 12]}})
$nin
// nin
//Does not match the values in the array
db.person.find({qty : {$nin: [2,322]}})
1.2 logical operators
$or
//Or conditional query
db.person.find({$or: [{age: {$lt: 20}},{address: "beijing"}]})
$and
//And condition query
db.person.find({$and: [{age: {$lt: 20}},{address: "beijing"}]})
$not
//Query documents that do not match expressions
db.person.find({age: {$not: {$gt: 20}}});
$nor
//Query documents that do not match any of the expressions
Db.col.find ({$nor: [{age: 20}, {sex: "male"}]})
1.3 element operators
$exists
//Query documents with specified fields
//To query documents with phone field:  
db.col.find({phone: {$exists: true}})
$type
//The query type is the document of the specified type, and the alias alias is added in version 3.2. The number and alias of various types are as follows

Mongodb revisits the road (2)

//Suppose there is
{ "_id": 1, address: "2030 Martian Way",zipCode: "90698345"},  
{ "_id": 2, address: "156 Lunar Place",zipCode: 43339374},  
{ "_id": 3, address: "2324 Pluto Place",zipCode: NumberLong(3921412)},  
{ "_id": 4, address: "55 Saturn Ring", zipCode: NumberInt(88602117)} 

//Query the documents with string data type of zipcode field in the collection:
db.col.find({zipCode: {$type: "string"}});
db.addressBook.find( { "zipCode" : { $type : 2 } } );
1.4 element operators
$mod
//Remainder condition query
//Query the document with the value of age field divided by 2 and remaining 0:  
db.person.find( { age: {$mod : [2, 0] } } )
$regex
//Regular expression query 
db.person.find({sku: {   $regex: /^ABC/i  }})
$text
//Text index query
{  
    $text: {  
        $search: <string>,  
        $language: <string>,  
        $caseSensitive: <boolean>,  
        $diacriticSensitive: <boolean>  
    }  
}

//Please refer to the official documents for details
$where
//Can support character or function
db.myCollection.find( { $where: "this.credits == this.debits" } );  
db.myCollection.find( { $where: function() { return obj.credits == obj.debits; } } );
1.5 array operators
$all
//The document that contains all the specified elements in the array field of the matching document

Query the documents whose tags field (an array) in the articles collection contains "SSL" and "security" (including, but not all equal to)  
db.articles.find({tagS: { $all:[ ["aal", "dsada"] ] }  })
$elemMatch()
Matches partial fields in an embedded document or array
//Suppose there is
{ _id: 1, results: [ 82, 85, 88 ] }  
{ _id: 2, results: [ 75, 88, 89 ] }  


db.col.find({results: { $elemMatch: { $gte: 80, $lt: 85} }  });
//Query the documents with interval [80,85] elements in the results array (the result is the first one);
$size
//Matches documents whose array length is the specified size
Query the documents that have collected 5 lucky cards:  
db.person.find({card:{$size:5}})
1.6 projection operator
$(projection)
//Query the element of the first matching condition in the array, which is equivalent to the findone() method
{ "_id" : 1, "semester" : 1, "grades" : [ 70, 87, 90 ] }  
{ "_id" : 2, "semester" : 1, "grades" : [ 90, 88, 92 ] }  
{ "_id" : 3, "semester" : 1, "grades" : [ 85, 100, 90 ] }  
{ "_id" : 4, "semester" : 2, "grades" : [ 79, 85, 80 ] }  
{ "_id" : 5, "semester" : 2, "grades" : [ 88, 88, 92 ] }  
{ "_id" : 6, "semester" : 2, "grades" : [ 95, 90, 96 ] } 

db.student.find({semester : 1 ,  grades: {$gte: 85}}, {"grades.$": 1});
Query semester = 1, and the first element in grades that conforms to the elements greater than or equal to 85:

//The result is
{ "_id" : 1, "grades" : [ 87 ] }
{ "_id" : 2, "grades" : [ 90 ] }
{ "_id" : 3, "grades" : [ 85 ] }
$(projection)
{  
 _id: 1,  
 zipcode: "63109",  
 students: [  
              { name: "john", school: 102, age: 10 },  
              { name: "jess", school: 102, age: 11 },  
              { name: "jeff", school: 108, age: 15 }  
           ]  
}  
{  
 _id: 2,  
 zipcode: "63110",  
 students: [  
              { name: "ajax", school: 100, age: 7 },  
              { name: "achilles", school: 100, age: 8 },  
           ]  
}  
{  
 _id: 3,  
 zipcode: "63109",  
 students: [  
              { name: "ajax", school: 100, age: 7 },  
              { name: "achilles", school: 100, age: 8 },  
           ]  
} 
//Query the documents with zipcode 63109 and school = 102 in the students array:

db.school.find({ zipcode: "63109" }, {student: { $elemMatch: { school: 102 } } } )

// result
{ "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
{ "_id" : 3 }
$slice
//Slice array in query (similar to pagination)
(1) In the query results, only the first five elements of the comments array are displayed  
db.posts.find( {}, { comments: { $slice: 5 } } )  
(2) In the query results, only the last five elements of the comments array are displayed  
db.posts.find( {}, { comments: { $slice: -5 } } )  
(3) In the query results, skip the top 20 elements of the comments array and display only the 10 elements (21-30)  
db.posts.find( {}, { comments: { $slice: [ 20, 10 ] } } )  
(4) Similarly, skip the last 20 and display 10:  
db.posts.find( {}, { comments: { $slice: [ -20, 10 ] } } )

Skip and limit

limit

//Get 20
db.things.find().limit(20);

limit

//Skip 20. Select 20 more
db.things.find().skip(20).limit(20)

sort

DB. Things. Find (). Sort ({"likes": - 1}) - 1 means reverse order