Go教程

MongoDB从入门到实战(五):MongoDB 查询find

本文主要是介绍MongoDB从入门到实战(五):MongoDB 查询find,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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 小于 $gt 大于

// 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

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

正则表达式和数组

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

$and

$or

$and 和 $or

这篇关于MongoDB从入门到实战(五):MongoDB 查询find的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!