MongoDB query document stepped pit remember!

1 count the number of

Statistical recording conditions using the count () method.
The number of records following statement statistics spit collection:

db.comment.count()

Conditional statistics, such as statistics for the number of records userid 1013:

db.comment.count({userid:"1013"})

2 fuzzy query

MongoDB fuzzy query is achieved by means of regular expressions.

format

/模糊查询字符串/

Review the query contains the "flow" of all documents, as follows:

db.comment.find({content:/流量/})
db.comment.find({content:/^加班/})

Greater than 3 is not equal to less than

<, <=,>,> = This operator is also very common.

format

db.集合名称.find({ "field" : { $gt: value }}) // 大 于: field > value 
db.集合名称.find({ "field" : { $lt: value }}) // 小 于: field < value 
db.集合名称.find({ "field" : { $gte: value }}) // 大于等 于: field >= value 
db.集合名称.find({ "field" : { $lte: value }}) // 小于等 于: field <= value 
db.集合名称.find({ "field" : { $ne: value }}) // 不等 于: field != value

Comments inquiry point is greater than the number of records like 1000:

db.comment.find({thumbup:{$gt:1000}})

4 contains does not contain

It comprises the use of $inoperator

Query comments set userid field contains 1013 documents and 1014:

db.comment.find({userid:{$in:["1013","1014"]}})

It does not include the use of $ operator Nin

Query comments set userid field does not contain 1013 documents and 1014:

db.comment.find({userid:{$nin:["1013","1014"]}})

Condition 5 is connected

If we need to query meet two or more conditions, the operator need to use the $ and the condition is associated with
(the equivalent of SQL and).

format

$and:[ {条件},{条件},{条件} ]

Query comments set thumbup than or equal to 1000 and less than 2000 documents:

db.comment.find({$and:[ {thumbup:{$gte:1000}} ,{thumbup: {$lt:2000} }]})

If a relationship between two or more conditions, we use the operator associating with the front and using
the same manner as in the format:

$or:[ {条件},{条件},{条件} ]

Query comments set userid to 1013, the number of points or less than praise documented 2000:

db.comment.find({$or:[ {userid:"1013"} ,{thumbup:{$lt:2000} }]})

6 value growth

On a column value increased or decreased on the basis of the original value, may be used $incoperators:

db.comment.update({_id:"2"},{$inc:{thumbup:1}})
Published 380 original articles · won praise 543 · views 330 000 +

Guess you like

Origin blog.csdn.net/qq_33589510/article/details/104929956