MongoDB查询文档

作者: 缘来是你ylh | 来源:发表于2019-03-06 00:03 被阅读45次

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

db.collection.find()语法

db.collection.find( <query filter>, <projection> )
  • query filter 查询条件,指明返回哪些文档
  • projection 查询映射,指定查询的文档要返回哪些字段

<query filter>为空的时候就是{},可以省略。所以db.collection.find()等价于db.collection.find({})

接着上一篇的实战,我们先来看一下users集合现在还有哪一些文档

> db.users.find()
{ "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" }
{ "_id" : ObjectId("5c77731d37955b945af73222"), "name" : "Hanmeimei", "age" : 17, "gender" : "female" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1 }

下面我们通过命令来演示find()方法的用法

1.查询符合条件的文档

如:查询age>25的全部文档

> db.users.find({"age": {$gt: 25}})
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1 }

2.查询符合条件的文档的指定字段

如:查询age>25的人的name

> db.users.find({"age": {$gt: 25}}, {name:1})
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy" }

如我们所愿返回了name字段,但是同时也返回了_id字段。_id是默认返回的,如果不想要它返回许指定_id:0

> db.users.find({"age": {$gt: 25}}, {name:1, _id:0})
{ "name" : "Jack" }
{ "name" : "Lucy" }

3.指定AND条件

如:查询年龄大于25的男性

> db.users.find({"age": {$gt: 25}, "gender": "male"})
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 }

4.指定OR条件

如:查询age=18的或者name是tom的文档

> db.users.find({$or: [{"name":"tom"}, {"age":18}]})
{ "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" }
5.指定AND和OR条件

如一下面的筛选满足2个条件

  • 年龄大于18
  • 女性或者名字叫"Jack"
> db.users.find({"age":{$gt: 18}, $or:[{"gender":"female"}, {"name":"Jack"}]})
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1 }

如果某一个字段对应的是数组,我们可以修改数组里面的某一个字段吗?

先给每一个文档插入一个字段points,它对应的值是个数组

> var points={[ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ]}
> db.users.updateMany({"age":{$gt:0}}, {$set:{"points":points}})
{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
> tmp=[{"points":90, "bonus":20}, {"points":88, "bonus":11}]
[
    {
        "points" : 90,
        "bonus" : 20
    },
    {
        "points" : 88,
        "bonus" : 11
    }
]
> db.users.update({"name": "tom"}, {$set: {"points": tmp}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.users.find().pretty()
> db.users.find().pretty()
{
    "_id" : ObjectId("5c77499137955b945af73220"),
    "name" : "tom",
    "age" : 20,
    "gender" : "male",
    "points" : [
        {
            "points" : 90,
            "bonus" : 20
        },
        {
            "points" : 88,
            "bonus" : 11
        }
    ]
}
{
    "_id" : ObjectId("5c77499137955b945af73221"),
    "name" : "Linda",
    "age" : 18,
    "gender" : "female",
    "points" : [
        {
            "points" : 85,
            "bonus" : 20
        },
        {
            "points" : 85,
            "bonus" : 10
        }
    ]
}
{
    "_id" : ObjectId("5c77731d37955b945af73222"),
    "name" : "Hanmeimei",
    "age" : 17,
    "gender" : "female",
    "points" : [
        {
            "points" : 85,
            "bonus" : 20
        },
        {
            "points" : 85,
            "bonus" : 10
        }
    ]
}
{
    "_id" : ObjectId("5c7774b736397a1c0aa425ea"),
    "name" : "Jack",
    "age" : 28,
    "gender" : "male",
    "is_active" : 1,
    "points" : [
        {
            "points" : 85,
            "bonus" : 20
        },
        {
            "points" : 85,
            "bonus" : 10
        }
    ]
}
{
    "_id" : ObjectId("5c7774b736397a1c0aa425eb"),
    "name" : "Lucy",
    "age" : 28,
    "gender" : "female",
    "is_active" : 1,
    "points" : [
        {
            "points" : 85,
            "bonus" : 20
        },
        {
            "points" : 85,
            "bonus" : 10
        }
    ]
}

这里我想查询points字段里面第一个points为90的

> db.users.find({"points.0.points": 90}).pretty()
{
    "_id" : ObjectId("5c77499137955b945af73220"),
    "name" : "tom",
    "age" : 20,
    "gender" : "male",
    "points" : [
        {
            "points" : 90,
            "bonus" : 20
        },
        {
            "points" : 88,
            "bonus" : 11
        }
    ]
}

不指定数组索引匹配字段

> db.users.find({"points.bonus": 11}).pretty()
{
    "_id" : ObjectId("5c77499137955b945af73220"),
    "name" : "tom",
    "age" : 20,
    "gender" : "male",
    "points" : [
        {
            "points" : 90,
            "bonus" : 20
        },
        {
            "points" : 88,
            "bonus" : 11
        }
    ]
}

只有points数组里面的bonus满足11就返回

还可以指定数组文档的多个查询条件,这里我就不掩饰了

6.null存在性筛选

> db.student.find()
{ "_id" : 1, "name" : "tom", "hobby" : null }
{ "_id" : 2, "name" : "Jack" }
> db.student.find({"hobby":null})
{ "_id" : 1, "name" : "tom", "hobby" : null }
{ "_id" : 2, "name" : "Jack" }
>

注意一下,我们筛选 hobby为null的,没有hobby这个字段的夜筛选出来了

> db.student.find({"hobby":{$exists:false}})
{ "_id" : 2, "name" : "Jack" }
> db.student.find({"hobby":{$exists:true}})
{ "_id" : 1, "name" : "tom", "hobby" : null }

通过$exists来指定

其他查询文档的方法 findOne()看方法名就知道值查询返回满足条件的第一条记录

> db.users.findOne({"age": {$gt: 20}})
{
    "_id" : ObjectId("5c7774b736397a1c0aa425ea"),
    "name" : "Jack",
    "age" : 28,
    "gender" : "male",
    "is_active" : 1,
    "points" : [
        {
            "points" : 85,
            "bonus" : 20
        },
        {
            "points" : 85,
            "bonus" : 10
        }
    ]
}

相关文章

  • spring date mongo mongotemplate使

    Spring数据MongoDB三:基本文档查询(查询,基本查询)(一) MongoDB高级查询[聚合] sprin...

  • mongo二2018-06-29

    MongoDB基础语法——查询数据 基本查询 find([{文档条件}]):全集合查询 findOne([{文档条...

  • MongoDB - 文档查询

    1.基本查询 查询集合中所有文档 使用游标遍历集合 2.条件查询 2.1 条件查询支持按照指定的字段值精准匹配 2...

  • MongoDB 查询文档

    语法MongoDB 查询数据的语法格式如下: 该语句是查询collection中全部数据,效果和关系型数据库的语句...

  • MongoDB查询文档

    MongoDB 查询文档使用 find() 方法。find() 方法以非结构化的方式来显示所有文档。 db.col...

  • MongoDB查询文档

    1.查询所有文档 语法:db.集合名字.find() 2.根据条件查找匹配的文档 语法:db.集合名字.find(...

  • MongoDB实现地理位置查询

    Mongodb地理位置查询文档MongoDB支持地理位置索引,可以直接用于位置距离计算和查询。查询结果默认将会由近...

  • PHP MongoDB 查询文档 --- 2022-04-02

    本章介绍PHP MongoDB文档查询的用法。 前置教程 MongoDB教程[https://www.tizi36...

  • MongoDB 文档查询 I

    文档查询 语法: pretty() 方法可将结果格式化为可读模式 query 条件 字段比较 AND 条件 格式:...

  • MongoDB查询内嵌文档

    一、概述 二、查询整个文档 例如:有如下文档 参考示例:查询date 为2016-08-05 user_id 为U...

网友评论

    本文标题:MongoDB查询文档

    本文链接:https://www.haomeiwen.com/subject/yworuqtx.html