在MongoDB中有两种方式查询数据库里的数据,即CURD查询和聚合管道查询。本章将对CURD查询进行详细讲解。

准备数据

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" } ]);

查询全部的inventory数据

db.inventory.find( {} ) //相当于select* from inventory

查询item是paper的数据

db.inventory.find( { status: "D" } ) //条件就是在find中使用{ <field1>: <value1>, ... }进行匹配

结果:

mongodb 数据分析(系统学习MongoDB四史上最全MongoDB查询API讲解)(1)

多个条件and

db.inventory.find( { status: "A", qty: { $lt: 30 } } )//多个条件and即同时制定多个field //上行语句中的$lt为操作符,意思是小于 整个条件的查询为查询 //select * from inventory where status =“A”and qty<30

多个条件or

db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } ) //使用$or操作符

既有and又有or的条件时

db.inventory.find( { status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ] } ) //mongo条过滤时支持正则 使用/ ^p/指item满足以p开头的 像sql中的like =“q%”

上边是逻辑操作符的使用,接下来展示比较运算符的使用。

//等于$eq { field: <value> } //默认为等于 也可以写成 { <field>: { $eq: <value> } } //不等于$ne db.inventory.find( { status: {$ne :"D" } }) //大于$gt 大于等于$gte 小于$lt 小于等于$lte 不等于 $ne db.inventory.find( { qty: {$gt :12} } ) //查出qty大于12的文档 //$in 同sql中的in意为满足值在条件中的任意一个 $nin 意为not in db.inventory.find( { status: {$in :["D","A"] } }) //查询状态是D 或者A的

除了关系数据库有的逻辑运算符和比较运算符之外,mongo还有很多独有的能力。Mongo可以查询存在某个字段的或者不存在某个字段的文档如下:

//插入一条数据 db.inventory.insertOne([ { item: "journal2", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A",from:"Chinese"} ]); //查询文档中含有from字段的文档 我们使用$exists操作符就可以做到 db.inventory.find({ from{$exists: true }}) //这得益于Mongo的松散结构,我们的文档不需要每个字段都一致。

当我们使用嵌套的文档格式时,还需要学习下面的查询操作

//还是我们刚才准备的数据,查询size 的宽是21 高是14的文档 db.inventory.find( { "size.h": 14 },{ "size.w": 21 } ) //字段为对象类型时条件字段我们可以使用.来连接子属性

我们的文档还可以存储数组类型的数据,数组类型的查询我们接着看:

//我们再插入几条数据 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 ] } ]); //查询tags中包含red的文档 db.inventory.find( { tags: "red" } ) ///查询tags同时含有 red和blank的文档 db.inventory.find( { tags: { $all: ["red", "blank"] } } ) //查询数组中包含的元素任意一个大于15且任意一个小于20的文档满足条件的可以不是同一个 db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } ) //查询数组中元素至少一个元素大于22小于30两个条件都满足 db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )// $elemMatch过滤每个元素 //查询数组中指定元素满足条件的文档 db.inventory.find( { "dim_cm.0": { $gt: 25 } } ) //查询数组中第一个元素满足条件的 //查询数组的元素个数满足条件的文档 db.inventory.find( { "dim_cm": { $size: 25 } } )

下图为执行结果:

mongodb 数据分析(系统学习MongoDB四史上最全MongoDB查询API讲解)(2)

查询结果

mongodb 数据分析(系统学习MongoDB四史上最全MongoDB查询API讲解)(3)

上图结果对比一下可以清晰理解$elemMatch和普通写法的区别。

当数组中的元素是对象时我们可以参考下面的查询操作:

//准备数据 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 } ] } ]); //查询数组对象中属性满足条件的文档 db.inventory.find( { 'instock.qty': { $lte: 20 } } ) //查询数组中指定元素的属性满足条件的文档 db.inventory.find( { 'instock.0.qty': { $lte: 20 } } ) //数组中只要一个元素满足条件的文档 db.inventory.find( { "instock": { qty: 5, warehouse: "A" } } ) //使用$elemMatch查询数组中任意一个元素都满足$elemMatch的条件 db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )

注意:db.inventory.find( { "instock": { qty: 5, warehouse: "A" } } )这个查询中嵌套文档的字段顺序也要和条件相同才会查出来,这正好和上一张讲的BSON文档的字段是有序的对应了起来。

下章整理result的操作符和update API的讲解。。

,