Collection query data operations |
Select * from tab |
Db. tab. find () Db. tab. find ({}) |
Select id from tab |
Db. tab. find ({}, {"id": 1}) # ({condition },{ field: 0/1 }) Db. tab. find ({},{ "_ id": 0, "id": 1 }) Db. tab. aggregate ({$ project: {id: 1 }}); Db. tab. find ({id: {$ exists: 1 }}); |
Select * from tab where id = 1 |
Db. tab. find ({id: 1 }) |
Select id from tab where id = 1 |
Db. tab. find ({id: 1 },{ "_ id": 0, "id": 1 }) |
Select * from tab where id <> 1 |
Db. tab. find ({id: {$ ne: 1 }}) |
Select * from tab where id> 2 |
Db. tab. find ({id: {$ gt: 2 }}) |
Select * from tab where id <3 |
Db. tab. find ({id: {$ lt: 3 }}) |
Select * from tab where id> = 2 |
Db. tab. find ({id: {$ gte: 2 }}) |
Select * from tab where id <= 3 |
Db. tab. find ({id: {$ lte: 3 }}) |
Select * from tab where id = 1 or id = 2 |
Db. tab. find ({$ or: [{id: 3}, {id: 2}]}) |
Select * from tab where id <2 or id> 4 |
Db. tab. find ({$ or: [{id: {$ gt: 4 },{ id: {$ lt: 2}]}) |
Select * from tab where id in (1, 2) |
Db. tab. find ({id: {$ in: [1, 2]}) |
Select * from tab where id not in (2, 3) |
Db. tab. find ({id: {"$ nin": [2, 3]}) |
Select * from tab where id between 2 and 3 Select * from tab where id> = 2 and id <= 3 |
Db. tab. find ({$ and: [{id: {$ gte: 2 },{ id: {$ lte: 5}]}) |
Select * from tab where id = 1 and name = 'kk' |
Db. tab. find ({id: 2, name: 'kk '}) |
Select distinct id from tab |
Db. tab. distinct ("id "); Db. runCommand ({distinct: "tab", key: "id "}) |
Select distinct id from tab where name = 'A' |
Db. runCommand ({distinct: 'tab', key: 'id', query: {name: 'A '}}) |
Select * from tab where name like '% A %' |
Db. tab. find ({name: {$ regex: ""}}) Db. tab. find ({name://}) |
Select * from tab order by id asc |
Db. tab. find (). sort ({id: 1 }) |
Select * from tab order by id desc |
Db. tab. find (). sort ({id:-1 }) |
Select top 5 * from tab |
Db. tab. find (). limit (5) |
Skip the first m records |
Db. tab. find (). skip (2) Db. tab. aggregate ({$ skip: 2 }); |
Skip the first m records and take n records from m + 1 |
Db. tab. find (). skip (2). limit (3) |
All columns except the specified columns are displayed. |
Db. tab. find ({id: null }) Db. tab. find ({}, {"_ id": 0 }) |
Search for rows whose field id is string (refer to the following table) |
Db. tab. find ({id: {$ type: 2 }}); |
|
|
Select name, sum (id) as sumid From tab Where id> 0 group by name |
Db. tab. group ({ Key: {"name": true} # group by name , Cond: {id: {$ gt: 0 }}# where id> 0 , Reduce: function (obj, prev) # Aggregate function {Prev. sumid + = obj. id;} # function logic, accumulate id , Initial: {sumid: 0 }}) # Initialization |
Select sum (*) from tab |
Db. tab. group ({key :{}, cond :{} , Reduce: function (obj, prev) {Prev. sumid + = obj. id;}, initial: {sumid: 0 }}); |
Select sum (*) as newcol from tab |
Db. tab. aggregate ([{$ group: {_ id: "$ by_user", newcol: {$ sum: "$ id" }}}]) |
Select count (*) from tab |
Db. tab. count () or db. tab. find (). count () |
Select count (*) from tab |
Db. tab. group ({key :{}, cond :{}, Reduce: function (obj, prev) {Prev. sumid + = 1 ;}, initial: {sumid: 0 }}); |
Select avg (*) from tab |
Db. tab. aggregate ([{$ group: {_ id: "$ by_user", newcol: {$ avg: "$ id" }}}]) |
Select max (*) from tab |
Db. tab. find (). sort ({id:-1}). limit (1) Db. tab. aggregate ([{$ group: {_ id: "$ by_user", newcol: {$ max: "$ id" }}}]) |
Select min (*) from tab |
Db. tab. find (). sort ({id: 1}). limit (1) Db. tab. aggregate ([{$ group: {_ id: "$ by_user", newcol: {$ min: "$ id" }}}]) |
|
|
# Element Query # Db. tab. insert ({ratings: [5, 8, 9]}) Db. tab. find ({ratings: [5, 8, 9]}) # find A matched Array Db. tab. find ({ratings: 5}) # search for records containing "5" in the element Db. tab. find ({ratings: {$ elemMatch: {$ gt: 8, $ lt: 10 }}) # search for element matching # Embedded document # Db. tab. insert ({producer: {company: 'abc', address: 'street '}}) # Db. tab. insert ({producer: [{company: 'abc', address: 'street'}, {company: 'kk ', address: 'street2'}]}) Db. tab. find ({producer: {company: 'abc', address: 'street '}}) Db. tab. find ({'producer. company': 'abc '}) Db. tab. find ({'producer. 0. address': 'street'}) # The address of the first element of 'producer = 'street' |