Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 嵌套localField时的$lookup_Mongodb_Mongodb Query - Fatal编程技术网

Mongodb 嵌套localField时的$lookup

Mongodb 嵌套localField时的$lookup,mongodb,mongodb-query,Mongodb,Mongodb Query,MongoDB版本3.4.10(应用程序使用Meteor框架) 目标:在运行时根据需要将_id引用的文档聚合到包含文档中 我有材料,模型,和目录收藏,以及以下文档: 材料 { "_id" : "cf4KgXw7ZK6ukdzR7", "name" : "parquet_wood_mahogany" } { "_id" : "Mwp5eYYZ4GZzvZuoK", "name" : "top_square_chamfered", "type" : "top" } { "_id"

MongoDB版本3.4.10(应用程序使用Meteor框架)

目标:在运行时根据需要将_id引用的文档聚合到包含文档中

我有
材料
模型
,和
目录
收藏,以及以下文档:

材料

 { "_id" : "cf4KgXw7ZK6ukdzR7", "name" : "parquet_wood_mahogany" }
{
  "_id" : "Mwp5eYYZ4GZzvZuoK",
  "name" : "top_square_chamfered",
  "type" : "top"
}
{
  "_id" : "CqhS2m2RcLZ2Bm4eb",
  "name" : "skirt_square",
  "type" : "skirt"
}
{
  "_id" : "dYP22ajALnWBwpBj2",
  "name" : "leg_square",
  "type" : "leg"
}
型号

 { "_id" : "cf4KgXw7ZK6ukdzR7", "name" : "parquet_wood_mahogany" }
{
  "_id" : "Mwp5eYYZ4GZzvZuoK",
  "name" : "top_square_chamfered",
  "type" : "top"
}
{
  "_id" : "CqhS2m2RcLZ2Bm4eb",
  "name" : "skirt_square",
  "type" : "skirt"
}
{
  "_id" : "dYP22ajALnWBwpBj2",
  "name" : "leg_square",
  "type" : "leg"
}
目录

{
  "_id" : "EcRGzPAq79giYKrbY",
  ...,
  "specs" : {
    ...,
    "models" : [
      {
        "mesh" : "Mwp5eYYZ4GZzvZuoK",
        "material" : "cf4KgXw7ZK6ukdzR7"
      },
      {
        "mesh" : "CqhS2m2RcLZ2Bm4eb",
        "material" : "cf4KgXw7ZK6ukdzR7"
      },
      {
        "mesh" : "dYP22ajALnWBwpBj2",
        "material" : "cf4KgXw7ZK6ukdzR7"
      }
    ]
  }
}
所需的聚合后返回的文档格式:

{
  "_id" : "EcRGzPAq79giYKrbY",
  ...,
  "specs" : {
    "dimensions" : {
      ...,
    },
    "models" : [
      {
        "mesh" : {
          "_id" : "Mwp5eYYZ4GZzvZuoK",
          "name" : "top_square_chamfered",
          "type" : "top"
        },
        "material" : {
          "_id" : "cf4KgXw7ZK6ukdzR7",
          "name" : "parquet_wood_mahogany"
        }
      },
      {
        "mesh" : {
          "_id" : "CqhS2m2RcLZ2Bm4eb",
          "name" : "skirt_square",
          "type" : "skirt"
        },
        "material" : {
          "_id" : "cf4KgXw7ZK6ukdzR7",
          "name" : "parquet_wood_mahogany"
        }
      },
      {
        "mesh" : {
          "_id" : "dYP22ajALnWBwpBj2",
          "name" : "leg_square",
          "type" : "leg"
        },
        "material" : {
          "_id" : "cf4KgXw7ZK6ukdzR7",
          "name" : "parquet_wood_mahogany"
        }
      }
    ]
  }
}
我没有包含任何查询代码,因为它离标记太远了,只不过是噪音。我一直在尝试使用
aggregate
,使用
$lookup
组合,但我没有达到我想要的效果。MongoDB v3.6
管道
语法将使这更容易。。。但我在v3.4中完全不知所措

我希望尽可能避免使用多个数据库请求来组合这些信息。如果您能提供任何建议,我们将不胜感激

编辑:工作解决方案-

db.catalog.aggregate([
  { "$lookup": {
    "from": 'models',
    "localField": "specs.models.mesh",
    "foreignField": "_id",
    "as": "models.mesh"
  }},
  { "$lookup": {
    "from": 'materials',
    "localField": "specs.models.material",
    "foreignField": "_id",
    "as": "models.material"
  }},
  { "$unwind": "$models.mesh" },
  { "$unwind": "$models.material" },
  { "$group":{
    "_id": "$_id",
    "title": { "$first": "$title" },
    "desc": { "$first": "$desc" },
    "thumbnail": { "$first": "$thumbnail" },
    "createdBy": { "$first": "$createdBy" },
    "createdAt": { "$first": "$createdAt" },
    "specs": { "$first": "$specs" },
    "models": { "$push": "$models" }
  }},
  { "$project": {
    "_id": "$_id",
    "title": "$title",
    "desc": "$desc",
    "thumbnail": "$thumbnail",
    "createdBy": "$createdBy",
    "createdAt": "$createdAt",
    "specs.dimensions": "$specs.dimensions",
    "specs.models": "$models",
  }}
])

你可以在下面试试

db.catalog.aggregate([
  { "$lookup": {
    "from": 'models',
    "localField": "specs.models.mesh",
    "foreignField": "_id",
    "as": "models.mesh"
  }},
  { "$lookup": {
    "from": 'materials',
    "localField": "specs.models.material",
    "foreignField": "_id",
    "as": "models.material"
  }},
  { "$unwind": "$models.mesh" },
  { "$unwind": "$models.material" },
  { "$group":{
    "_id": "$_id",
    "title": { "$first": "$title" },
    "desc": { "$first": "$desc" },
    "thumbnail": { "$first": "$thumbnail" },
    "createdBy": { "$first": "$createdBy" },
    "createdAt": { "$first": "$createdAt" },
    "specs": { "$first": "$specs" },
    "models": { "$push": "$models" }
  }},
  { "$project": {
    "title": "$title",
    "desc": "$desc",
    "thumbnail": "$thumbnail",
    "createdBy": "$createdBy",
    "createdAt": "$createdAt",
    "specs.dimensions": "$specs.dimensions",
    "specs.models": "$models",
  }}
])

谢谢你的回复。我正在尝试并调整您的查询,但没有返回任何结果
Models.collection.name
Materials.collection.name
更改为各自的名称<代码>模型和
材料
分别。可能您的
模型.集合.名称
/
材料.集合.名称
不正确。。。。否则它应该会起作用,我正在进行调整,它正在走到一起。你给我指明了正确的方向。models.mesh和models.materials需要规格。在他们面前。不需要第一个$REWIND。我马上会在这里再次发帖,差一点就明白了。@KyleRichardson明白了我做错了什么。。。请查看最新的答案,这就是我目前正在努力工作的内容。虽然它抛出了一个错误“specs字段必须是累加器对象”,但我发布了一个与我使用的查询相关的编辑。