【问题标题】:Mongodb's $match returns multiple occurrences of the same documentMongodb 的 $match 返回同一文档的多次出现
【发布时间】:2015-10-25 21:10:55
【问题描述】:

我的文档结构如下:

{
name: "some user name",
  cvs: [{
    title: 'Cv title'
    technologies: [
      {
        text: 'JavaScript',
        main: true
      },
      {
        text: "AngularJs",
        main: true
      }
    ]
  }]
}

当我进行以下聚合时(db 中只有一个文档):

db.users.aggregate([

{"$unwind": "$cvs"},
{"$unwind": "$cvs.technologies"},
{"$match": {
    "cvs.isBlocked": false,
    "cvs.moderated": true,
    "cvs.isVisible": true,
    "cvs.technologies.main": true 
    }
 },
  {"$project": {
      "type": "$cvs.occupationType",
      "proficiency": "$cvs.proficiencyLevel",
      "_id": "$cvs._id",
      "title": "$cvs.title",
      "technologies": "$cvs.technologies.text",
     }}
])

我得到一个包含两个元素的数组(但这是同一个文档),只是因为有两种技术匹配

"cvs.technologies.main": true

[
    {
        "_id" : ObjectId("5629e813279b62fe075fbd4c"),
        "type" : "Frontend",
        "proficiency" : "Middle",
        "title" : "Frontend developer",
        "technologies" : "JavaScript",
    },
    {
        "_id" : ObjectId("5629e813279b62fe075fbd4c"),
        "type" : "Frontend",
        "proficiency" : "Middle",
        "title" : "Frontend developer",
        "technologies" : "Ruby",
    }
]

我怎样才能得到这个结果?:

[
    {
        "_id" : ObjectId("5629e813279b62fe075fbd4c"),
        "type" : "Frontend",
        "proficiency" : "Middle",
        "title" : "Frontend developer",
        "technologies" : ["JavaScript", "Ruby"],
    }
]

【问题讨论】:

  • 请显示与您收藏的文档结构相同的示例文档。我认为有更好的方法来做到这一点。

标签: mongodb mongodb-query database


【解决方案1】:

运行以下管道,其中添加了额外的 $match$group 管道步骤,以便 optimize 聚合管道(在 $unwind 之前放置一个 $match 管道以过滤掉通过管道的不需要的文档)获得所需的结果(使用 $group strong> 运算符按给定字段对文档进行分组,使用 $push 累加器创建数组):

db.users.aggregate([
    {
        "$match": {
            "cvs.isBlocked": false,
            "cvs.moderated": true,
            "cvs.isVisible": true,
            "cvs.technologies.main": true 
        }
    },
    {"$unwind": "$cvs"},
    {"$unwind": "$cvs.technologies"},
    {
        "$match": {
            "cvs.isBlocked": false,
            "cvs.moderated": true,
            "cvs.isVisible": true,
            "cvs.technologies.main": true 
        }
    },
    {
        "$group": {
            "_id": {
                "type": "$cvs.occupationType",
                "proficiency": "$cvs.proficiencyLevel",
                "_id": "$cvs._id",
                "title": "$cvs.title"               
            },
            "technologies": {
                "$push": "$cvs.technologies.text"
            }
        }
    },
    {
        "$project": {
            "type": "$_id.type",
            "proficiency": "$_id.proficiency",
            "_id": "$_id._id",
            "title": "$_id.title",
            "technologies": 1,
        }
    }
])

【讨论】:

  • 这就是诀窍!但是结果中缺少“类型”和“熟练程度”。只有标题和技术存在
  • @user2814599 更新答案,错字。现在应该可以工作了:-)
  • 我应该在问之前检查一下是否找到错字!。有用!谢谢!
  • 您能告诉我,我如何将来自“用户”集合本身(具有上述嵌套文档)的数据包含到 $project 中?例如。用户名。
  • 您能为此创建一个新问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 2018-03-08
相关资源
最近更新 更多