Collection Querying 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 ( |
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: "A"}}) Db.tab.find ({name:/a/}) |
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 previous M notes |
Db.tab.find (). Skip (2) Db.tab.aggregate ({$skip: 2}); |
Skip the previous m record, starting from m+1 to fetch n |
Db.tab.find (). Skip (2). Limit (3) |
In addition to the specified columns, the other columns are displayed |
Db.tab.find ({id:null}) Db.tab.find ({},{"_id": 0}) |
Find a row with a field ID of type string (refer to the table below) |
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) #聚合函数 {Prev.sumid + = obj.id;} #函数逻辑, accumulate ID , initial: {sumid:0}}) #初始化 |
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"}}]) |
|
|
#元素查询 #db. Tab.insert ({ratings: [5, 8, 9]}) Db.tab.find ({ratings: [5, 8, 9]}) #查找匹配的数组 Db.tab.find ({ratings:5}) #查找元素中含 record for "5" Db.tab.find ({ratings:{$elemMatch: {$gt: 8, $lt: Ten}}}) #元素匹配查找 #内嵌文档 #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 '}) #字段 ' producer's first element of address= ' street ' |