When it comes to queries, we generally think of relational database queries, such as order by (sort), limit (paging), range query (greater than a value, less than a value), in queries, on queries, like queries waiting a lot, and MongoDB also supports these operations, Just different syntax, such as the sort: MongoDB inside using the Skip (field:1/-1) method, the following to introduce:
A. MongoDB Query document
1. Query the document find (): No condition is to query the collection of all the data
Syntax: Db.collectionName.find ({condition},{field:true/false})
1.1 Querying all data
Displays the specified field (with contrast, find () the second parameter is the field of the specified query, the format [field name: True/false]true is displayed, False is not displayed):
1.2 Query total data count ()
Syntax: Db.collectionName.find ({condition}). Count ()
2. Querying a Document FindOne (): Default is the first document that the query meets the criteria
Syntax: Db.collectionName.findOne ({condition},{field:true/false})
3.Pretty () method: Format The document that displays the query
The conditional operators ("$and", "$nor", "$not", "$or", "$exists", "$mod", "$regex", "$where", "$slice ", "$elemMatch", "null query", "$all", "$size", "$in", "$nin")
1. The following is a list of conditional operators, meaning, syntax, and comparison with relational databases
Equals |
{<key>:<value> } |
db.col.find({"by":"zgw"}) |
where by = ‘zgw‘ |
Less than ($LT) |
{<key>:{$lt:<value>}} |
db.col.find({"likes":{$lt:50}}) |
where likes < 50 |
Less than or equal to ($lte) |
{<key>:{$lte:<value>}} |
db.col.find({"likes":{$lte:50}}) |
where likes <= 50 |
Greater than ($GT) |
{<key>:{$gt:<value>}} |
db.col.find({"likes":{$gt:50}}) |
where likes > 50 |
Greater than or equal to ($GTE) |
{<key>:{$gte:<value>}} |
db.col.find({"likes":{$gte:50}}) |
where likes >= 50 |
Not equal to ($ne) |
{<key>:{$ne:<value>}} |
db.col.find({"likes":{$ne:50}}) |
where likes != 50 |
Syntax: Db.collectionName.find ({field:{operators1:value1,operators2:value2}})--[operators: These are the operators above ()]
PHP Format: Array (Field=>array (' $lte ' =>value)) or array (Field=>array (' $gte ' =>value1, ' $lte ' =>value2))
The following is a query that is not equal to an operator:
2. operators:"$and", "$nor", "$not", "$or", "$exists", "$mod", "$regex", "$where", "$slice", " $elemMatch "," null query "," $all "," $size "," $in "," $nin "
2.1 $and :
Continued: http://www.cnblogs.com/egger/p/3135847.html
Http://www.cnblogs.com/zhaoyang/archive/2012/01/10/2317664.html
MongoDB Query Document