资讯专栏INFORMATION COLUMN

MongoDB学习笔记(3)- Mongo Shell 常用查询命令

mykurisu / 759人阅读

摘要:通过这个基本可以实现任意类型的查询。三使用正则查询查找名字中含有的记录查找以名称以开头且不区分大小写的记录四查询数组判断某个数组类型字段包含的多个指定值时。

MongoDB学习笔记(3)- Mongo Shell 常用查询命令
本文所使用的MongoDB版本为 4.0.10
> db.version();
4.0.10
一、find 命令进行简查询
find( 查询条件 ,返回的字段), 
1. 查询时返回所有字段

db.user.find() --> 查询user集合中所有的数据

> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

db.user.find( {"username":"Mary"} ) --> 列出username=Mary的数据

> db.user.find({"username": "Mary"})
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }

db.user.find( {"age":50} ) --> 列出 age = 50 的数据

> db.user.find( {"age":50} )
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
2. 查询时只返回指定几个字段

db.user.find({}, {"username":1}) --> 列出所有人的 username 字段

> db.user.find({}, {"username": 1})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary" }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin" }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart" }

db.user.find({}, {"tel": 1}) --> 列出所有人的 tel 字段

> db.user.find({}, {"tel": 1})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7") }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8") }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9") }

db.user.find({} , {"age": 0} ) --> 除了age字段,其他字段都列出来

> db.user.find({}, {"age": 0})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary" }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin" }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart" }
二、常用操作符 1. $lt (<)

查询年龄小于30岁的用户

> db.user.find( {"age": { $lt: 30 } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
2. $lte (<=)

查询年龄小于等于30岁的用户

> db.user.find( {"age": { $lte: 30 } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
3. $gt (>)

查询年龄大于30岁的用户

> db.user.find( {"age": { $gt: 30 } } )
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
4. $gte (>= )

查询年龄大于等于30岁的用户

> db.user.find( {"age": { $gte: 30 } } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
5. $ne ( <> )

查询年龄不等于30岁的用户

> db.user.find( {"age": { $ne: 30 } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
6. $and (AND)

查询年龄大于10岁且小于40岁的用户

> db.user.find( { $and: [ { "age": { $lt: 40 } }  , {"age": { $gt: 10 } } ] } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
7. $or (OR)

查询年龄小于20岁或者大于40岁的用户

> db.user.find( { $or: [ { "age": { $lt: 20 } }  , {"age": { $gt:40 } } ] } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
8. $in (IN)

查询年龄为 20, 30, 40 岁的用户

> db.user.find( { "age" : { $in : [20, 30, 40] } } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
9. $not (NOT)

查询年龄不是 20, 40 岁的用户($not)

> db.user.find( { "age" : { $not: { $in : [20, 40] } } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
10. $nin (NOT IN)

查询年龄不是 20, 40 岁的用户

> db.user.find( { "age" : { $nin : [20, 40] } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
11. $mod (取模)

查询年龄对 4 取模余 2 的用户

> db.user.find( {"age" : { $mod : [4, 2] } } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
12. $exists (存在)
$exists 用于判断键是否

给 Mary 添加 tel 字段,值设为 null

> db.user.update({ "username": "Mary" }, { $set : { "tel" : null } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

列出存在 tel 字段的记录

> db.user.find({ "tel" : { $exists : 1 }})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
判断值为 null 时要注意:
db.user.find({ "friends" : null })
该命令不仅查出值为null的记录,friends 键不存在的记录也会被取出来。
> db.user.find({ "tel" : null })
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
如果想取出 friends 键存在,并且值为 null 的记录应该这样来取:
> db.user.find( { $and: [ { "tel" : null }, { "tel" : {$exists: 1 } } ] } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
13. $where
$where : 根据函数返回值来判断是否返回数据

取出 年龄 * 3 + 5 小于 100 用户的

db.user.find( { $where : function(){ 
    return (this.age * 3 + 5 < 100); 
} } );
> db.user.find( { $where : function(){ return (this.age * 3 + 5 < 100); } } );
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
this 代表当前这个记录。

通过这个 $where 基本可以实现任意类型的查询。不过不到必要时候不要用这个方法,因为它的速度比一般查询要慢很多。

三、使用正则查询

查找名字中含有 "art" 的记录

> db.user.find( { "username" : /art/ } )
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

查找以名称以 "mar" 开头且不区分大小写的记录

> db.user.find( {"username": /^mar/i } );
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
四、查询数组 1. $all:判断某个数组类型字段包含的多个指定值时。

给 user 集合中的用户添加一些朋友,结果如下:

> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086", "friend" : [ "Mary", "Jocker", "Martin", "Kart" ] }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null, "friend" : [ "Jocker", "Martin" ] }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40, "friend" : [ "Mary", "Jocker", "Kart" ] }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

取出 friend 数组中既有 Mary 又有 Jocker 的记录

> db.user.find({ "friend": { $all : ["Mary", "Jocker"] } })
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086", "friend" : [ "Mary", "Jocker", "Martin", "Kart" ] }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40, "friend" : [ "Mary", "Jocker", "Kart" ] }
2. $size:查询拥有指定元素个数的数组

取出 friend 数组中有3个值的记录

> db.user.find( { "friend" : { $size  : 3 } } )
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40, "friend" : [ "Mary", "Jocker", "Kart" ] }
3. $slice : 返回一个数组的子集

{$slice : 10} --> 数组中的前10个
{$slice : -10} --> 数组中的后10个
{$slice : [20, 10] } --> 从数组中下标为20的元素开始,向后去除10个元素

取出Tom的前3个朋友

> db.user.find( {"username": "Tom"}, { "friend" : {$slice : 3} } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086", "friend" : [ "Mary", "Jocker", "Martin" ] }
五、查询内嵌文档 1. $elemMatch

如有这样的数据结构:
有个comments字段,该字段是一个数组,每一项是一个内嵌的comment对象

{
    "_id": ObjectId("5d31b1d24fd0d7ad0a1a1361"),
    "author": "Tom",
    "content": "I am Tom!",
    "comments": [{
        "user": "Mary",
        "score": 3,
        "comment": "Nice!"
    }, {
        "user": "Martin",
        "score": 6,
        "comment": "I"m reading..."
    }, {
        "user": "Jocker",
        "score": 8,
        "comment": "You"re kidding me"
    }]
}
{
    "_id": ObjectId("5d31b3114fd0d7ad0a1a1364"),
    "author": "Martin",
    "content": "I am Martin!",
    "comments": [{
        "user": "Tom",
        "score": 5,
        "comment": "Nice!"
    }, {
        "user": "Mary",
        "score": 6,
        "comment": "I"m reading..."
    }, {
        "user": "Jocker",
        "score": 3,
        "comment": "You"re kidding me"
    }]
}
{
    "_id": ObjectId("5d31b3314fd0d7ad0a1a1365"),
    "author": "Mary",
    "content": "I am Mary!",
    "comments": [{
        "user": "Tom",
        "score": 3,
        "comment": "Nice!"
    }, {
        "user": "Martin",
        "score": 5,
        "comment": "I"m reading..."
    }, {
        "user": "Jocker",
        "score": 2,
        "comment": "You"re kidding me"
    }]
}

查询评论中包含 score 大于5的文章记录

> db.blog.find ({ "comments" : { "$elemMatch" : { "score" : {"$gt" : 5}}} })
{ "_id" : ObjectId("5d31b1d24fd0d7ad0a1a1361"), "author" : "Tom", "content" : "I am Tom!", "comments" : [ { "user" : "Mary", "score" : 3, "comment" : "Nice!" }, { "user" : "Martin", "score" : 6, "comment" : "I"m reading..." }, { "user" : "Jocker", "score" : 8, "comment" : "You"re kidding me" } ] }
{ "_id" : ObjectId("5d31b3114fd0d7ad0a1a1364"), "author" : "Martin", "content" : "I am Martin!", "comments" : [ { "user" : "Tom", "score" : 5, "comment" : "Nice!" }, { "user" : "Mary", "score" : 6, "comment" : "I"m reading..." }, { "user" : "Jocker", "score" : 3, "comment" : "You"re kidding me" } ] }

查询Tom的文章的评论中 score 大于 5 的评论记录

> db.blog.find ({"author": "Tom"}, { "comments" : { "$elemMatch" : { "score" : {"$gt" : 3}}} })
{ "_id" : ObjectId("5d31b1d24fd0d7ad0a1a1361"), "comments" : [ { "user" : "Martin", "score" : 6, "comment" : "I"m reading..." } ] }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/106197.html

相关文章

  • MongoDB 学习笔记

    摘要:本文内容主要来自的学习,学习笔记基于个人理解对原书部分内容进行调整。如果需要练习相关命令行工具可直接阅读本学习笔记。笔者测试数据库版本较早,但文中涉及的所有概念及命令行工具基本适用于所有版本。二准备安装和运行服务在学习之前,需要安装环境。 感谢 Karl Seguin 编写的 The Little MongoDB Book 这本 MongoDB 入门书。 本文内容主要来自「The Li...

    Juven 评论0 收藏0
  • MongoDB 学习笔记

    摘要:本文内容主要来自的学习,学习笔记基于个人理解对原书部分内容进行调整。如果需要练习相关命令行工具可直接阅读本学习笔记。笔者测试数据库版本较早,但文中涉及的所有概念及命令行工具基本适用于所有版本。二准备安装和运行服务在学习之前,需要安装环境。 感谢 Karl Seguin 编写的 The Little MongoDB Book 这本 MongoDB 入门书。 本文内容主要来自「The Li...

    bitkylin 评论0 收藏0
  • MongoDB 学习笔记

    摘要:本文内容主要来自的学习,学习笔记基于个人理解对原书部分内容进行调整。如果需要练习相关命令行工具可直接阅读本学习笔记。笔者测试数据库版本较早,但文中涉及的所有概念及命令行工具基本适用于所有版本。二准备安装和运行服务在学习之前,需要安装环境。 感谢 Karl Seguin 编写的 The Little MongoDB Book 这本 MongoDB 入门书。 本文内容主要来自「The Li...

    makeFoxPlay 评论0 收藏0
  • MongoDB 学习笔记

    摘要:本文内容主要来自的学习,学习笔记基于个人理解对原书部分内容进行调整。如果需要练习相关命令行工具可直接阅读本学习笔记。笔者测试数据库版本较早,但文中涉及的所有概念及命令行工具基本适用于所有版本。二准备安装和运行服务在学习之前,需要安装环境。 感谢 Karl Seguin 编写的 The Little MongoDB Book 这本 MongoDB 入门书。 本文内容主要来自「The Li...

    刘永祥 评论0 收藏0
  • MongoDB学习笔记(1)- MongoDB简介、数据类型及帮助命令

    摘要:数据模型取决于数据库类型。仅支持位浮点数,所以位整数会被自动转换为位浮点数。位浮点数中的数字都是这种类型。数字只能表示为双精度数位浮点数的另外一个问题是,有些位的整数并不能精确地表示为位浮点数。 MongoDB学习笔记(1)- MongoDB简介及数据类型 本文所使用的MongoDB版本为 4.0.10 > db.version(); 4.0.10 一、MongoDB 介绍 1. Mo...

    nihao 评论0 收藏0
  • mongodb学习笔记

    摘要:我们常说的分表分库分区等概念都属于分片的实际体现。传统分片做法是手工分表分库。自动分片技术是根据指定的片键自动拆分数据并维护数据请求路由的过程。 1.mongodb特性 1)mongo是一个面向文档的数据库,它集合了nosql和sql数据库两方面的特性。 2)所有实体都是在首次使用时创建。 3)没有严格的事务特性,但是它保证任何一次数据变更都是原子性的。 4)也没有固定的数据模型 5)...

    王晗 评论0 收藏0

发表评论

0条评论

mykurisu

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<