find() 

  • MongoDB 中查询文档使用 find()
  • find() 方法以非结构化的方式来显示所要查询的文档

 

语法格式

db.collection.find(query, projection)
  • query:可选项,设置查询操作符指定查询条件
  • projection :可选项,指定要在与 query 匹配的文档中返回的字段,如果忽略此选项则返回所有字段

 

pretty()

为了查看文档的格式更加直观美丽,可以最后加个 pretty() 方法

db.inventory.find().pretty()
{
    "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"),
    "item" : "journal",
    "qty" : 25,
    "size" : {
        "h" : 14,
        "w" : 21,
        "uom" : "cm"
    },
    "status" : "A"
}

注意:findOne() 是没有的跟 pretty() 方法的

 

findOne()

和 find() 的都是查询文档,但是只返回匹配查询条件成功的第一个文档

 

语法格式

db.collection.findOne(query, projection)

 

查询条件

MongoDB 支持查询条件操作符,下表为 MongoDB 与 RDBMS(关系型数据库,Mysql)常见的查询条件操作符的对比

操作符 格式 实例 与 RDBMS where 语句比较
等于(=) {<key> : {<value>}} db.test.find( {price : 24} ) where price = 24
大于(>) {<key> : {$gt : <value>}} db.test.find( {price : {$gt : 24}} ) where price > 24
小于(<) {<key> : {$lt : <value>}} db.test.find( {price : {$lt : 24}} ) where price < 24
大于等于(>=) {<key> : {$gte : <value>}} db.test.find( {price : {$gte : 24}} ) where price >= 24
小于等于(<=) {<key> : {$lte : <value>}} db.test.find( {price : {$lte : 24}} ) where price <= 24
不等于(!=) {<key> : {$ne : <value>}} db.test.find( {price : {$ne : 24}} ) where price != 24
与(and) {key01 : value01, key02 : value02, ...} db.test.find( {name : "小菠萝测试笔记", price : 24} ) where name = "小菠萝测试笔记" and price = 24
或(or) {$or : [{key01 : value01}, {key02 : value02}, ...]} db.test.find( {$or:[{name : "小菠萝测试笔记"},{price : 24}]7} ) where name = "小菠萝测试笔记" or price = 24
in {<key> : {$in : [a,b,c,d.....]}}

db.test.find( { qty: { $in: [ 5, 15 ] } } )

where qty in (5,15)
not in {<key> : {$nin : [a,b,c,d.....]}} db.test.find( { qty: { $nin: [ 5, 15 ] } } ) where qty not in (5,15)

 

插入测试数据

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( {} )
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9e"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa0"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

 

等价 Mysql 的写法

SELECT * FROM inventory

 

查询文档,= 等于操作

语法格式

{ <field1>: <value1>}

 

实际栗子

> db.inventory.find( { status: "D" } )
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa0"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }

 

等价 Mysql 的写法

SELECT * FROM inventory WHERE status = "D"

 

查询操作符

这里有一个概念叫查询操作符,其实就是上面查询条件列的那些栗子

 

使用查询操作符的语法格式

{ <field1>: { <operator1>: <value1> }, ... }

还有哪些查询操作符后面再展开详解

 

查询文档,and 与操作

> db.inventory.find( { status: "A", qty: { $lt: 30 } } )
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }

  

等价 Mysql 的写法

SELECT * FROM inventory WHERE status = "A" AND qty < 30

  

查询文档,in 操作

> db.inventory.find( { status: { $in: [ "A", "D" ] } } )
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9e"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa0"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

 

等价 Mysql 的写法

SELECT * FROM inventory WHERE status in ("A", "D")

 

查询文档,or 或操作

> db.inventory.find({$or:[{status:"A"},{qty:{$gt:50}}]})
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9e"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa0"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

  

等价 Mysql 的写法

SELECT * FROM inventory WHERE status = "A" OR qty > 50

 

查询文档,and 加 or 的操作

查询文档选择集合中 status 为“A”、qty小于($lt)30或 item 以字符 p 开头的所有文档

db.inventory.find( {
     status: "A",
     $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )

MongoDB 支持正则表达式

 

等价 Mysql 的写法

SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")