MongoDB 查询
db.collectionName.find(query, projection)
- query :可选,使用查询操作符指定查询条件
- projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值,只需省略该参数即可(默认省略)
例
const cursor = db.collection('inventory').find({ status: 'D' });
若要以易读的方式读取数据,可以用 pretty()
:
db.collectionName.find().pretty()
使用投影返回特定的字段
选择集合中的所有文档
将空文档作为查询过滤器参数传递给 find
方法
db.collectionName.find( {} )
对应于
SELECT * FROM collectionName
仅返回 指定的字段(和 _id 字段)
将投影文档中的 <field>
设置为 1
db.collectionName.find( { status: "A" }, { item: 1, status: 1 } )
对应于
SELECT _id, item, status from collectionName WHERE status = "A"
_id 默认是始终返回的;若要禁止返回默认的 _id
,添加 _id: 0
db.collectionName.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
返回除了被排除的字段之外的所有字段
可以使用 projection 来排除特定的字段,而不是列出要在匹配的文档中返回的字段
db.collectionName.find( { status: "A" }, { status: 0, instock: 0 } )
除
_id
字段之外,无法在投影文档中同时使用包含 1 与排除 0 声明。
返回嵌入式文档的特定字段
使用 .
返回嵌入式文档中的特定字段
以下示例将返回:
_id
(默认返回)、item
、status
、size
中的uom
字段(仍会嵌入在size
文档中)
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, 'size.uom': 1 });
您还可以使用嵌套表单指定嵌入式字段。例如:{ item: 1, status: 1, size: { uom: 1 } }
。
排除语法也是类似,形如 { 'size.uom': 0 }
或 { size: { uom: 0 } }
对数组中的 嵌入式文档的操作也类似,形如 { 'instock.qty': 1 }
对数组元素的指定返回操作
对于包含数组的字段,MongoDB 提供了用来操作数组的以下投影操作符:$elemMatch
、$slice
和$
。
如下示例使用 $slice
投影操作符返回 instock
数组中的最后一个元素:
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, instock: { $slice: -1 } });
比较操作符
操作 | 格式 | 范例 | RDBMS 中的类似语句 |
---|---|---|---|
等于 | {<key>:<value> } | db.col.find({"name":"Loulou"}) | where by = 'name' |
小于 | {<key>:{$lt:<value>}} | db.col.find({"likes":{$lt:50}}) | where likes < 50 |
小于或等于 | {<key>:{$lte:<value>}} | db.col.find({"likes":{$lte:50}}) | where likes <= 50 |
大于 | {<key>:{$gt:<value>}} | db.col.find({"likes":{$gt:50}}) | where likes > 50 |
大于或等于 | {<key>:{$gte:<value>}} | db.col.find({"likes":{$gte:50}}) | where likes >= 50 |
不等于 | {<key>:{$ne:<value>}} | db.col.find({"likes":{$ne:50}}) | where likes != 50 |
- AND 条件:条件间以逗号隔开即可
- OR 条件:使用关键字
$or
例子
db.inventory.find( { status: "D" } )
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
$type
操作符
类型 | 数字 | 备注 |
---|---|---|
Double | 1 | |
String | 2 | |
Object | 3 | |
Array | 4 | |
Binary data | 5 | |
Undefined | 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 | Query with -1 . |
Max key | 127 |
db.collection.find({"title": {$type: 2}})
查询数组
有例
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
{ item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
{ item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
{ item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
{ item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);