Original: http://blog.csdn.net/congcong68/article/details/46841075
First, Introduction
MongoDB provides a db.collection.find () method that can be implemented based on conditional queries and specifies that fields returned using the projection operator omit this parameter to return all fields in the matching document.
Two Db.collection.find () Querying data
Grammar
[SQL]View PlainCopy
- > Db.collection.find (query,projection)
Parameters |
Type |
Describe |
Query |
Document |
Optional. Using query operators to specify query criteria |
Projection |
Document |
Specifies that fields returned using the projection operator omit this parameter to return all fields in the matching document |
Projection syntax:
[SQL]View PlainCopy
- {field1: <boolean>, Field2: <boolean> ...}
Description
1 or True indicates the return field
0 or False indicates that the field is not returned
_ID: The default is 1, not specifying that the field will be returned by default, unless it is set to 0, the field will not be returned.
Specify the return field, sometimes the document field is many and the data is large, we specify to return the fields we need, so as to save the amount of data transferred, reduce memory consumption, improve performance, when the data is large, performance is obvious.
1. Querying data
(1) Returns all documents in the collection:
Db.collection.find ()
Or
Db.collection.find ({})
Like the SQL statement:
SELECT * from TABLENAME
(2) Specifies that fields returned using the projection operator omit this parameter to return all fields in the matching document
Grammar:
[SQL]View PlainCopy
- Db.orders.find ({},{field1: <boolean>, Field2: <boolean> ...})
Example:
[SQL]View PlainCopy
- >db.orders.find ({},{"Onumber": 1,"CNAME": 1})
And the _id default setting is 1, and all returns back
You can set _id not to return
Example:
[SQL]View PlainCopy
- >db.orders.find ({},{"Onumber": 1,"CNAME": 1,"_id": 0})
2. Search by condition
(1) equals conditional query
Grammar:
[SQL]View PlainCopy
- >db.collect.find ({<field1>: <value1>,<field2>: <value2>, .....})
Example:
[SQL]View PlainCopy
- >db.orders.find ({"onumber":"002"})
Find documents for ONUMBER=002
(2) comparison operators
$GT (greater than), $GTE (greater than or equal to), $lt (less than), $lte (less than or equal to)
Grammar:
[SQL]View PlainCopy
- {<field1>: {<expression1>},<field2>: {<expression1>}, ...}
1) $gt (greater than) comparison operator
Example:
[SQL]View PlainCopy
- >db.orders.find ({"Onumber": {$gt:"003"}})
We're looking for onumber>003 with a value of only 004.
2) $gte (greater than or equal to) union query with $lte (less than or equal to) and specify return field (via second parameter)
Example:
[Delphi]View PlainCopy
- >db. Orders. Find ({"Onumber": {$gte: "002", $lte: "003"}},{"Onumber": 1, "CNAME": 1})
We look for 002=<onumber<=003 and specify the return Onumber and CNAME fields, and the _id default setting is 1, and all returns back
(3) Search for $or, and $and conditions
1) Query $and conditions
Grammar:
[SQL]View PlainCopy
- {$ and: [{<expression1>}, {<expression2>}, ...,]}
The syntax for simple usage:
[SQL]View PlainCopy
- {<field1>: <value1>,<field2>: <value2>,. ...}
Example:
[SQL]View PlainCopy
- >db.orders.find ({"onumber":"002","CNAME":"Zcy2"})
Finding onumber=002 and Cname= zcy2 querying documents
2) $or (or) conditional query
Grammar:
[SQL]View PlainCopy
- {$nor: [{<expression1>}, {<expression2>}, ...]}
Example:
[SQL]View PlainCopy
- >db.orders.find ({$or:[{"onumber":"002"},{"CNAME":"Zcy1"}]})
Our conditions onumber=002 or cname= zcy1 find documents that meet onumber=002 or cname= zcy1 conditions
3) $or and $and joint condition query
Example:
[SQL]View PlainCopy
- >db.orders.find ({$and:[{"date":"2015-07-01"},{$or:[{"onumber":"002"},{"CNAME": "Zcy1"}]}]})
The document of the query condition Date=2015-07-01and (onumber=002 or cname=zcy1) is either equal to the date equals 2015-07-01 and to satisfy Onumber equals 002 or CNAME equals zcy1 one of them.
(4) $in (inclusive), $nin (not included) condition inquiry
1) $in (included) criteria query
Grammar:
[SQL]View PlainCopy
- {field: {$ in: [<value1>, < Value2>, ...]}}
Example:
[SQL]View PlainCopy
- >db.orders.find ({"Onumber": {$in:["001","002"]}})
The document that queries Onumber in ("001", "002") conditions is Onumber equals 001 or equals 002 This is a bit like $or, but when $or is a conditional query, you can specify a different field: Db.orders.find ({$or: [{" Onumber ":" 002 "},{" CNAME ":" Zcy1 "}]})
, and $in only for one field.
2) $nin (not included) criteria query
Grammar:
[SQL]View PlainCopy
- {field: {$nin: [<value1>, < Value2>, ...]}}
$nin (not included) the opposite of $in (inclusive), there is no specific introduction
(5) $not (not equal to) condition query
Grammar:
[SQL]View PlainCopy
- {field: {$ not: {< expression1>}}}
$not operator cannot be used independently and must be used with other operating conditions (except $regex)
Example:
[SQL]View PlainCopy
- >db.orders.find ({"Onumber": {$not:{$gt:"002" }}})
Find Onumber is not equal to more than 002 of document data
(6) $exists used to determine whether a field exists
Grammar:
[SQL]View PlainCopy
- {field: {$ exists: < boolean>}}
Example:
[SQL]View PlainCopy
- >db.orders.find ({"age": {$exists:true}})
Without the element of age, nothing comes back.
Insert an age element and perform a
(7) $mod modulo and equals the specified value
Grammar:
[SQL]View PlainCopy
- {field: {$mod: [Value, value2]}}
For the element field value, modulo the value of value to value2 the document data
Example:
[SQL]View PlainCopy
- >db.orders.find ({"age": {$mod: [5,1]}})
For the value of the age element and 5 modulo, the value of the modulo is equal to 1 of the document data
(8) Null lookup element does not exist and the element corresponds to a document with a value of NULL
Grammar:
{Field:null}
Example:
[SQL]View PlainCopy
- >db.orders.find ({"age":null})
1) Find the age element exists and the value equals null
[SQL]View PlainCopy
- >db.orders.find ({"age": {$in:[null], $exists:True}})
(9) $type to match the type of an element
Grammar:
{field: {$type: <;}}
Number is the type value corresponding to the type used in MongoDB
Type |
Type value |
Double |
1 |
String |
2 |
Object |
3 |
Array |
4 |
Binary data |
5 |
Undefined (deprecated) |
6 |
Object ID |
7 |
Boolean |
8 |
Date |
9 |
Null |
10 |
Regular Expression |
11 |
Javascript |
13 |
Symbol |
14 |
JavaScript (with scope) |
15 |
32-bit integer |
16 |
Timestamp |
17 |
64-bit integer |
18 |
Min Key |
255 |
Max Key |
127 |
Learn MongoDB Four: MongoDB query (i)