美文网首页程序员
MongoDB查询内嵌文档

MongoDB查询内嵌文档

作者: AQ王浩 | 来源:发表于2015-10-09 10:50 被阅读7103次

有两种方式可以查询内嵌文档

  • 查询整个文档

  • 针对键值对进行查询

查询整个文档

  db.emp.insert({
    "id":"A001",
    "name":{
        "first":"Carey",
        "last":"Ickes"
    },
    "age":25
  })
  db.emp.find({"name" : { "first" : "Carey", "last" : "Ickes" }})
> db.emp.find()
{ "_id" : ObjectId("561720d7de256e5153080ea7"), "id" : "A001", "name" : { "first" : "Carey", "last" : "Ickes" }, "age" : 25 }
> bb = db.emp.findOne({ "_id" : ObjectId("561720d7de256e5153080ea7") })
{
    "_id" : ObjectId("561720d7de256e5153080ea7"),
    "id" : "A001",
    "name" : {
        "first" : "Carey",
        "last" : "Ickes"
    },
    "age" : 25
}
> db.emp.find({"name" : { "first" : "Carey", "last" : "Ickes" }})
{ "_id" : ObjectId("561720d7de256e5153080ea7"), "id" : "A001", "name" : { "first" : "Carey", "last" : "Ickes" }, "age" : 25 }
> bb.name = { "first" : "Carey", "last" : "Ickes", "middle" : "Joe" }
{ "first" : "Carey", "last" : "Ickes", "middle" : "Joe" }
> db.emp.save(bb)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.emp.find({"name" : { "first" : "Carey", "last" : "Ickes" }})

这种查询会去精确匹配整个内嵌文档。如果其中name元素的子集发生变化,查询条件不与整个内嵌文档相匹配。
而且这种查询,还与顺序有关系。

键值对查询

查询文档时,可以用"."来表示进入内嵌文档

> db.emp.find({ "name.last" : "Ickes", "name.first" : "Carey" })
{ "_id" : ObjectId("561720d7de256e5153080ea7"), "id" : "A001", "name" : { "first" : "Carey", "last" : "Ickes", "middle" : "Joe" }, "age" : 25 }

现在,如果Ickes增加了更多的键,这个查询依然会匹配他的姓跟名。

数组里包含内嵌文档的查询


db.blog.insert({
    "_id":"B001",
    "title":"MongoDB查询",
    "comments":[
      {
        "name":"ickes",
        "score":3,
        "comment":"nice"
      },
      {
        "name":"xl",
        "score":4,
        "comment":"nice"
      },
      {
        "name":"eksliang",
        "score":5,
        "comment":"nice"
      },
      {
        "name":"ickes",
        "score":6,
        "comment":"nice"
       }
    ]
})


db.blog.insert({
    "_id":"B002",
    "title":"MySQL查询",
    "comments":[
      {
        "name":"MySQL-ickes",
        "score":4,
        "comment":"nice"
      },
      {
        "name":"MySQL-xl",
        "score":9,
        "comment":"nice"
      },
      {
        "name":"MySQL-eksliang",
        "score":5,
        "comment":"nice"
      },
      {
        "name":"MySQL-ickes",
        "score":6,
        "comment":"nice"
       }
    ]
})

# 以下两种查询都是精确匹配文档,所以查询不出结果
> db.blog.find({ "comments" : { "name": "ickes", "score" : { "$gt" : 5 } } })
> db.blog.find({ "comments" : { "name": "ickes", "score" : { "$gt" : 5 }, "comment":"nice" } })

#
查询条件里面的键值对会解释为AND,但是对于数组的内嵌文档来说它会解释为OR的关系。也就是
说上面的解释会拆解为  comments.name : ickes 或者 comments.score : { "$gt" : 5 }
> db.blog.find({ "comments.name" : "ickes", "comments.score" : { "$gt":5 } })
{ "_id" : "B001", "title" : "MongoDB查询", "comments" : [ { "name" : "ickes", "score" : 3, "comment" : "nice" }, { "name" : "xl", "score" : 4, "comment" : "nice" }, { "name" : "eksliang", "score" : 5, "comment" : "nice" }, { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }

查询 "name" : "ickes", "score" : { "$gt" : 5 }

> db.blog.find({"comments" : { "$elemMatch" : { "name" : "ickes", "score" : { "$gt" : 5 } } }})
{ "_id" : "B001", "title" : "MongoDB查询", "comments" : [ { "name" : "ickes", "score" : 3, "comment" : "nice" }, { "name" : "xl", "score" : 4, "comment" : "nice" }, { "name" : "eksliang", "score" : 5, "comment" : "nice" }, { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }

查询 "score" : { "$gt" : 5 }

> db.blog.find({"comments" : { "$elemMatch" : { "score" : { "$gt" : 5 } } }})
{ "_id" : "B001", "title" : "MongoDB查询", "comments" : [ { "name" : "ickes", "score" : 3, "comment" : "nice" }, { "name" : "xl", "score" : 4, "comment" : "nice" }, { "name" : "eksliang", "score" : 5, "comment" : "nice" }, { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }
{ "_id" : "B002", "title" : "MySQL查询", "comments" : [ { "name" : "MySQL-ickes", "score" : 4, "comment" : "nice" }, { "name" : "MySQL-xl", "score" : 9, "comment" : "nice" }, { "name" : "MySQL-eksliang", "score" : 5, "comment" : "nice" }, { "name" : "MySQL-ickes", "score" : 6, "comment" : "nice" } ] }

返回与查询条件相匹配的任意一个数组元素

> db.blog.find({
    "comments" : {
      "$elemMatch" : {
        "name" : "ickes",
        "score" : { "$gt" : 5 }
      }
    }
},
{
  "comments.$" : 2
}
)

> { "_id" : "B001", "comments" : [ { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }
> qq = db.blog.findOne({ "_id" : "B002" })

> qq.comments = [
        {
            "name" : "ickes",
            "score" : 9,
            "comment" : "nice"
        },
        {
            "name" : "MySQL-xl",
            "score" : 9,
            "comment" : "nice"
        },
        {
            "name" : "MySQL-eksliang",
            "score" : 5,
            "comment" : "nice"
        },
        {
            "name" : "MySQL-ickes",
            "score" : 6,
            "comment" : "nice"
        }
    ]

> db.blog.save(qq)


再次执行

db.blog.find({
    "comments" : {
      "$elemMatch" : {
        "name" : "ickes",
        "score" : { "$gt" : 5 }
      }
    }
},
{
  "comments.$" : 2
}
)

> db.blog.find({ "comments" : { "$elemMatch" : { "name" : "ickes", "score" : { "$gt" : 5 } } } }, { "comments.$" : 2 })
{ "_id" : "B001", "comments" : [ { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }
{ "_id" : "B002", "comments" : [ { "name" : "ickes", "score" : 9, "comment" : "nice" } ] }
>

最后再执行

db.blog.find({
    "comments" : {
      "$elemMatch" : {
        "name" : "ickes",
        "score" : { "$gt" : 5 }
      }
    }
},
{
  "comments.$" : 1
}
)

{ "_id" : "B001", "comments" : [ { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }
{ "_id" : "B002", "comments" : [ { "name" : "ickes", "score" : 9, "comment" : "nice" } ] }

注意只有一个查询条件,就可以不使用$elemMatch
下面两个查询是等价的

db.blog.find({ "comments.score" : { "$gt" : 5 } })
db.blog.find({ "comments" : { "$elemMatch" : { "score" : { "$gt" : 6 } } } })

$elemMatch 的用法

  • 元素匹配
db.nums.insert([{ "results" : [ 82, 85, 88 ] },{ "results" : [ 75, 88, 89 ] }])

查询 80 <= x < 85 的数据,数组中的任何一个元素都要
大于等于80并且小于等于85

> db.nums.find({ "results" : { "$elemMatch" : { "$gte" : 80, "$lt" : 85 } } })

{ "_id" : ObjectId("5617320ade256e5153080ea9"), "results" : [ 82, 85, 88 ] }

  • 数组内嵌文档
db.survey.insert([
{ _id: 1, results: [ { product: "abc", score: 10 }, { product: "xyz", score: 5 } ] },
{ _id: 2, results: [ { product: "abc", score: 8 }, { product: "xyz", score: 7 } ] },
{ _id: 3, results: [ { product: "abc", score: 7 }, { product: "xyz", score: 8 } ] }
{ _id: 4, results: [ { product: "abc", score: 10 }, { product: "xyz", score: 5 } ] },
{ _id: 5, results: [ { product: "abc", score: 8 }, { product: "xyz", score: 7 } ] },
{ _id: 6, results: [ { product: "abc", score: 7 }, { product: "xyz", score: 8 } ] }
])

查询 product 为 xyz,并且score 大于 8 的集合

> db.survey.find({ results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } })
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] }
{ "_id" : 6, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] }
>   db.survey.find({ results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } }, { "results.$" : 1 })
{ "_id" : 3, "results" : [ { "product" : "xyz", "score" : 8 } ] }
{ "_id" : 6, "results" : [ { "product" : "xyz", "score" : 8 } ] }

  • 单一查询条件

如果只有单一的查询条件,那么可以不用 $elemMatch

db.survey.find({
  results: { $elemMatch: { product: "xyz" } }
})

等同于

db.survey.find(
  { "results.product": "xyz" }
)

相关文章

  • MongoDB查询内嵌文档

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

  • MongoDB查询内嵌文档

    有两种方式可以查询内嵌文档 查询整个文档 针对键值对进行查询 查询整个文档 这种查询会去精确匹配整个内嵌文档。如果...

  • Mongodb 查询内嵌文档

    在books这个集合里有一个document存储的结构如图一所示,想要查找指定id的document里面指定id的...

  • MongoDB 对嵌套(Embedded)数组的过滤查询

    前言 MongoDB 推荐使用「内嵌文档(Embedded)」,所以带来一个问题,如何查询嵌入文档内的数据? 假如...

  • MongoDB查询(数组、内嵌文档和$where)

    查询数组 查询数组很容易,对于数组,我们可以这样理解:数组中每一个元素都是这个键值对键的一个有效值,如下面的例子:...

  • mongodb之mgo操作bson入门

    mongo内嵌文档相关的查询语句

  • spring date mongo mongotemplate使

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

  • mongo二2018-06-29

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

  • MongoDB学习 (六):查询

    目录 查询操作 集合查询方法 find() 查询内嵌文档 查询操作符(内含 数组查询) "$gt" 、"$gte"...

  • MongoDB - 文档查询

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

网友评论

    本文标题:MongoDB查询内嵌文档

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