MongoDB从入门到实战(六):MongoDB 查询文档 find

x33g5p2x  于2021-12-25 转载在 Go  
字(9.9k)|赞(0)|评价(0)|浏览(529)

db.collection.find()

查找所有文档

// 查找所有文档,相当于 select * from user
> db.user.find()
// pretty() : 用于美化返回值,每个key:value 各占一行
> db.user.find().pretty()
{
	"_id" : 1,
	"username" : "a"
}

// find 肯定也支持带条件的查询, 相当于select * from user where username = 'a'
> db.user.find({"username": "a"})
{ "_id" : 2, "username" : "a" }

// 查询指定字段,1:代表要查询的字段,0:代表不要查询的字段
// select username from user where username = 'a'
> db.user.find({"username": "a"}, {"_id": 0, "username": 1})
{ "username" : "a" }
{ "username" : "a" }

db.collection.findOne()

查询满足条件的第一条记录。

// select * from user limit 1
> db.user.findOne()
{ "_id" : 1, "username" : "a" }

> db.user.find()
{ "_id" : 1, "username" : "a" }
{ "_id" : 2, "username" : "a" }
// 返回满足条件的第一条
> db.user.findOne({"username": "a"})
{ "_id" : 1, "username" : "a" }

比较查询

  • $ lt 小于
  • $ lte 小于等于
  • $ gt 大于
  • $ gte 大于等于
  • $ ne 不等于
// select * from user where _id < 2
> db.user.find({"_id": {"$lt": 2}})
{ "_id" : 1, "username" : "a" }

// select * from user where _id > 1
> db.user.find({"_id": {"$gt": 1}})
{ "_id" : 2, "username" : "a" }

// 同一个字段多个条件
// select * from user where _id > 1 and _id < 3
> db.user.find({"_id": {"$gt": 1, "$lt": 3}})
{ "_id" : 2, "username" : "a" }

$in 和 $nin

// select * from user where _id in (1, 2)
> db.user.find({"_id": {"$in": [1, 2]}})
{ "_id" : 1, "username" : "a" }
{ "_id" : 2, "username" : "a" }

// select * from user where _id not in (1, 2)
> db.user.find({"_id": {"$nin": [1, 2]}})

$where

通过js函数function 自定义where查询条件,注意$where是不走索引的。

> db.user.find({$where: function() { return this.username == 'xiaohong'}})
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }

$and

and 用于连接多个条件,条件之间是and关系。

// 多个字段条件默认使用and作为连接
> db.user.find({"_id": 1, "username": "xiaohong"})
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }

// 显式使用 and 作为连接条件
> db.user.find({$and: [{"_id": 1}, {"username": "xiaohong"}]})
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }

$or

or 用于连接多个条件,条件之间使用or关系。

> db.user.find({$or: [{"_id": 2}, {"username": "xiaohong"}]})
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "money", "xiaojiejie" ] }

$and 和 $or

// select * from user where _id = 1 and (username = 'xiaohong' or hobby 包含 'money')
> db.user.find({"_id": 1, $or: [{"username": "xiaohong"}, {"hobby": "money"}]})
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }

正则表达式和数组

> db.user.remove({})
> db.user.insertMany([{ "_id" : 1, "username" : "xiaohong" },{ "_id" : 2, "username" : "xiaoming" }, { "_id" : 3, "username" : "mingMING" }])
{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3 ] }

// /^/表示以什么开头,即右模糊,相当于 select * from user where username like 'xiao%'
> db.user.find({"username": /^xiao/})
{ "_id" : 1, "username" : "xiaohong" }
{ "_id" : 2, "username" : "xiaoming" }

// /$/表示以什么结束,即左模糊,相当于 select * from user where username like '%ming'
> db.user.find({"username": /ming$/})
{ "_id" : 2, "username" : "xiaoming" }

// i : 表示忽略大小写
> db.user.find({"username": /ming$/i})
{ "_id" : 2, "username" : "xiaoming" }
{ "_id" : 3, "username" : "mingMING" }

// 左右模糊,select * from user where username like '%mi%'
> db.user.find({"username": /mi/})
{ "_id" : 2, "username" : "xiaoming" }
{ "_id" : 3, "username" : "mingMING" }

// 正则表达式用于数组
> db.user.find()
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "money", "xiaojiejie" ] }
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

> db.user.find({"hobby": /xiaojiejie/})
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "money", "xiaojiejie" ] }
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

// 正则表达式中包含变量需要使用eval()函数
> var ele = "xiaojiejie"
> db.user.find({"hobby": eval("/" + ele + "/")})
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "money", "xiaojiejie" ] }
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }
// 条件字段如果是数组,条件值为一个元素表示是否包含,包含该元素就查询出来
> db.user.find({"hobby": "xiaojiejie"})
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "money", "xiaojiejie" ] }
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

// 条件字段如果是数组,条件值为一个数组,此时表示的是精确匹配,不是包含关系
> db.user.find({"hobby": ["xiaojiejie"]})
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

// 数组中同时包含多个元素需要使用修饰符 $all
> db.user.find({"hobby": {"$all": ["xiaojiejie", "money"]}})
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "money", "xiaojiejie" ] }

// 使用下标作为数组条件,表示第i个元素的值是指定值的文档
> db.user.find({"hobby.0": "xiaojiejie"})
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

// 根据数组元素个数作为查询条件
> db.user.find({"hobby": {"$size": 1}})
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

// 获取数组中的前几个值,正数表示从前面开始获取
> db.user.find({}, {"hobby": {$slice: 1}})
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "money" ] }
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

// 获取数组中的后面几个值,负数表示从后开始获取
> db.user.find({}, {"hobby": {$slice: -1}})
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "xiaojiejie" ] }
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

// [index, count] 从第几个元素开始获取,获取几个元素
> db.user.find({}, {"hobby": {$slice: [0, 2]}})
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "money", "xiaojiejie" ] }
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

// $elemMatch: 数组中是否有一个元素同时满足所有条件(只要有一个元素满足就能匹配上)
> db.xyz.find({"x": {"$elemMatch": {"$gt": 3,"$lt": 6}}})
{ "_id" : 1, "x" : [ 2, 5 ] }
{ "_id" : 2, "x" : [ 4, 5 ] }
{ "_id" : 3, "x" : [ 4, 10 ] }

// 根据字段类型作为条件,2表示字符串类型
> db.coll.insertMany([{"x": "abc", "y": "10"}, {"x": 6, y: null}, {"x": new Date()}])
> db.coll.find({"x": {"$type": 2}})
{ "_id" : ObjectId("6073bb24daa0d45856bee570"), "x" : "abc", "y" : "10" }

// 字段的值为null或者没有该字段都会被视作null
> db.coll.find({"y": null})
{ "_id" : ObjectId("6073bb24daa0d45856bee571"), "x" : 6, "y" : null }
{ "_id" : ObjectId("6073bb24daa0d45856bee572"), "x" : ISODate("2021-04-12T03:14:44.376Z") }

// 查询字段为null,并且存在该字段
> db.coll.find({"y": {"$in": [null], "$exists": true}})
{ "_id" : ObjectId("6073bb24daa0d45856bee571"), "x" : 6, "y" : null }

distinct

获取某个字段所有不重复的值。

// select distinct id from user
> db.user.distinct("_id")
[ 1, 2, 3 ]

count

查询满足条件的文档总数量

// select count(*) from usesr
> db.user.find().count()
3

limit

返回满足条件的前N条文档。

> db.user.find()
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "money", "xiaojiejie" ] }
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

// select * from user limit 2
> db.user.find().limit(2)
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }
{ "_id" : 2, "username" : "xiaoming", "hobby" : [ "money", "xiaojiejie" ] }

skip

skip 跳过前N条文档,从第N + 1条开始取,skip和limit结合就是分页。

// select * from user limit 2, 1
> db.user.find().skip(2).limit(1)
{ "_id" : 3, "username" : "mingMING", "hobby" : [ "xiaojiejie" ] }

sort

sort 排序,1表示升续,-1表示降续。

skip(), limilt(), sort()三个放在一起执行的时候,执行的顺序是先 sort(), 然后是 skip(),最后是显示的 limit(),和命令编写顺序无关。

// select * from user order by id desc limit 2, 1
> db.user.find().sort({"_id": -1}).skip(2).limit(1)
{ "_id" : 1, "username" : "xiaohong", "hobby" : [ "dog" ] }

相关文章