db.inventory.insertMany([
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }]);
db.inventory.find( {} )
SELECT * FROM inventory
{ <field1>: <value1>, ... }
db.inventory.find( { status: "D" } )
SELECT * FROM inventory WHERE status = "D"
{ <field1>: { <operator1>: <value1> }, ... }
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
Note: 尽管可以使用 $or 操作符来满足上述需求,但是在对相同字段进行等值检索的时候更建议使用 $in 。
SELECT * FROM inventory WHERE status in ("A", "D")
1.4 AND 条件
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
SELECT * FROM inventory WHERE status = "A" AND qty < 30
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
SELECT * FROM inventory WHERE status = "A" OR qty < 30
Note: 使用比较操作符的查询受 Type Bracketing 的约束。
db.inventory.find( { status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]} )
SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
Note: MongoDB 支持正则表达式操作符 $regex 来做字符串模式匹配
其它的方法
-
db.collection.findOne -
在聚合管道中,$match 管道阶段提供了 MongoDB 的查询过滤。
Note: db.collection.findOne 方法提供了返回单个文档的读操作。 实际上,db.collection.findOne 就是 db.collection.find() 方法后面加了个限制条数1。
db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }]);
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )
Note: 当在查询语句中使用”.”,字段和嵌套文档字段必须在引号内。
2.1嵌套文档中的字段等值查询
db.inventory.find( { "size.uom": "in" } )
2.2 使用查询操作符查询
{ <field1>: { <operator1>: <value1> }, ... }
db.inventory.find( { "size.h": { $lt: 15 } } )
2.3 使用 AND 条件
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
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 ] }]);
db.inventory.find( { tags: ["red", "blank"] } )
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
3.1 查询数组中的元素
db.inventory.find( { tags: "red" } )
{ <array field>: { <operator1>: <value1>, ... } }
db.inventory.find( { dim_cm: { $gt: 25 } } )
3.2 多条件查询数组中的元素
3.3 使用多条件查询数组中的元素
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
3.4 数组中的元素同时满足多个查询条件
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
3.5 使用数组下标查询数组中的元素
Note: 当使用点号的时候,字段和嵌套文档字段必须在引号内
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
db.inventory.find( { "tags": { $size: 3 } } )
db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }]);
4.1 查询数组中的嵌套文档
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
db.inventory.find( { "instock": { qty: 5, warehouse: "A" } } )
db.inventory.find( { 'instock.qty': { $lte: 20 } } )
使用数组下标查询数组中嵌套文档中的字段
Note: 当查询使用点号的时候,字段和索引必须在引号内。
db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )
4.3 指定多个条件检索数组嵌套文档
单个嵌套文档中的字段满足多个查询条件
db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )
db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )
多个元素联合满足查询条件
db.iventory.find( { "instock.qty": { $gt: 10, $lte: 20 } } )
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )
db.inventory.insertMany( [
{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }]);
5.1 返回匹配文档中的所有字段
db.inventory.find( { status: "A" } )
SELECT * from inventory WHERE status = "A"
5.2仅返回指定字段和_id字段
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
SELECT _id, item, status from inventory WHERE status = "A"
5.3 去除 _id 字段
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
SELECT item, status from inventory WHERE status = "A"
Note: 除_id字段外,不能在映射文档中同时使用包含和去除语句。
5.4 去除指定字段
db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
Note: 除_id字段外,不能在映射文档中同时使用包含和去除语句。
5.5 返回嵌套文档中的指定字段
-
_id 字段(默认返回) -
item 字段 -
status 字段 -
文档 size 中的 uom 字段
db.inventory.find( { status: "A" },
{ item: 1, status: 1, "size.uom": 1 })
5.6 去除嵌套文档中的指定字段
db.inventory.find( { status: "A" }, { "size.uom": 0 })
5.7 映射数组中的嵌套文档的指定字段
-
_id 字段(默认返回) -
item 字段 -
status 字段 -
数组字段 instock 中的嵌套文档中的 qty 字段
db.inventory.find( { status: "A" },
{ item: 1, status: 1, "instock.qty": 1 } )
5.8 映射返回数组中指定的数组元素
db.inventory.find( { status: "A" },
{ item: 1, status: 1, instock: { $slice: -1 } } )
感谢大家一直以来对社区的关注与支持!社区在大家共同的努力下不断的发展与壮大,为了给大家营造更便捷的交流环境,QQ 技术交流群将同步在“微信技术交流群”中。添加小芒果微信发送“mongo”即可进入技术交流群。
评论前必须登录!
注册