Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Arrays 多个集合上的MongoDB客户端联接_Arrays_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Arrays 多个集合上的MongoDB客户端联接

Arrays 多个集合上的MongoDB客户端联接,arrays,mongodb,mongodb-query,aggregation-framework,Arrays,Mongodb,Mongodb Query,Aggregation Framework,我的收藏 预订-id,unitId 单位-id、标题、课程id 课程-id,标题 我想获得带有单元和课程标题的预订阵列,如下所示 博金·身份证单元·职称课程·职称 xxxx ABC XYZ 我当前的代码 const bookings = await db.getDB().collection('bookings').aggregate([ { $lookup: { from: 'courseUnit', localField: 'courseUni

我的收藏

预订-id,unitId

单位-id、标题、课程id

课程-id,标题

我想获得带有单元和课程标题的预订阵列,如下所示

博金·身份证单元·职称课程·职称

xxxx ABC XYZ

我当前的代码

const bookings   = await db.getDB().collection('bookings').aggregate([
  {
    $lookup:
    {
      from: 'courseUnit',
      localField: 'courseUnitId',
      foreignField: '_id',
      as: 'uu'
    }
  },
  {
      $unwind:"$uu"
  }
]).toArray();

您正在寻找的解决方案是通过在聚合管道中使用多时间
$lookup
管道来解决的。e、 g:

db.Bookings.aggregate([
    { 
        "$lookup" : 
            {"from":  "Unit", "localField": "unitId", "foreignField": "_id", as : "UnitDetails"   } 
    }, 
    {
        "$unwind" : "$UnitDetails"
    },
    {
        $lookup:   
            { "from": "Course", "localField": "UnitDetails.courseId", "foreignField": "_id", as: "CouserDetails"    }  
    },
    {
        "$unwind" : "$CouserDetails"
    },
    {   
        $project:  {_id:0, "Boking_id" : "$_id",  "Unit_title": "$UnitDetails.title", "course_title": "$CouserDetails.title" } 
    }   

]).pretty()
上述查询的输出如下:

{
    "Boking_id" : ObjectId("5dff07e4187da4c2b5ffc59f"),
    "Unit_title" : "unit title for course title 3",
    "course_title" : "Title of the Course collection 3"
}
> db.Bookings2.find()
{ "_id" : ObjectId("5dff6c2a187da4c2b5ffc5a3"), "unitId" : "5dff66c9187da4c2b5ffc5a2" }
> db.Unit2.find()
{ "_id" : ObjectId("5dff66c9187da4c2b5ffc5a2"), "title" : "Unit title 1", "courseId" : "5dff6694187da4c2b5ffc5a0" }
> db.Course2.find()
{ "_id" : ObjectId("5dff6694187da4c2b5ffc5a0"), "title" : "Title of the Course collection 1" }
{ "_id" : ObjectId("5dff6694187da4c2b5ffc5a1"), "title" : "Title of the Course collection 2" }
> 
> db.Bookings2.aggregate([ {$project: {"unitObjectId" : {$toObjectId: "$unitId"}} },  {$lookup:  {"from": "Unit2", "localField": "unitObjectId", "foreignField": "_id", as:  "UnitDetails" }  }, {$unwind: "$UnitDetails"}, {$project: {"courseObjectId":   {$toObjectId:  "$UnitDetails.courseId" }, "unitTitle": "$UnitDetails.title"  } }, {$lookup:  {"from": "Course2", "localField": "courseObjectId", "foreignField": "_id", as:  "Course2Details"  }  }, {$unwind: "$Course2Details"}, {$project: {_id: 0, "Boking_id": "$_id",  "Unit_title":  "$unitTitle", "course_title": "$Course2Details.title" }  }    ]).pretty()
{
    "Boking_id" : ObjectId("5dff6c2a187da4c2b5ffc5a3"),
    "Unit_title" : "Unit title 1",
    "course_title" : "Title of the Course collection 1"
}
> 
在所有三个集合中,我的初始集合数据集如下所示:

> db.Bookings.find()
{ "_id" : ObjectId("5dff07e4187da4c2b5ffc59f"), "unitId" : ObjectId("5dff06d6187da4c2b5ffc59e") }
> db.Unit.find()
{ "_id" : ObjectId("5dff06d6187da4c2b5ffc59e"), "title" : "unit title for course title 3", "courseId" : ObjectId("5dff010d187da4c2b5ffc59b") }
> db.Course.find()
{ "_id" : ObjectId("5dff0108187da4c2b5ffc599"), "title" : "Title of the Course collection" }
{ "_id" : ObjectId("5dff010b187da4c2b5ffc59a"), "title" : "Title of the Course collection 2" }
{ "_id" : ObjectId("5dff010d187da4c2b5ffc59b"), "title" : "Title of the Course collection 3" }
{ "_id" : ObjectId("5dff010f187da4c2b5ffc59c"), "title" : "Title of the Course collection 4" }
> 
有关详细信息,请参阅以下参考资料:

  • 如果Unit&Course集合中unitId和courseId的值存储为简单字符串,则必须使用。我创建了另一组集合,并使用了
    $toObjectId
    ,如下所示:

    {
        "Boking_id" : ObjectId("5dff07e4187da4c2b5ffc59f"),
        "Unit_title" : "unit title for course title 3",
        "course_title" : "Title of the Course collection 3"
    }
    
    > db.Bookings2.find()
    { "_id" : ObjectId("5dff6c2a187da4c2b5ffc5a3"), "unitId" : "5dff66c9187da4c2b5ffc5a2" }
    > db.Unit2.find()
    { "_id" : ObjectId("5dff66c9187da4c2b5ffc5a2"), "title" : "Unit title 1", "courseId" : "5dff6694187da4c2b5ffc5a0" }
    > db.Course2.find()
    { "_id" : ObjectId("5dff6694187da4c2b5ffc5a0"), "title" : "Title of the Course collection 1" }
    { "_id" : ObjectId("5dff6694187da4c2b5ffc5a1"), "title" : "Title of the Course collection 2" }
    > 
    > db.Bookings2.aggregate([ {$project: {"unitObjectId" : {$toObjectId: "$unitId"}} },  {$lookup:  {"from": "Unit2", "localField": "unitObjectId", "foreignField": "_id", as:  "UnitDetails" }  }, {$unwind: "$UnitDetails"}, {$project: {"courseObjectId":   {$toObjectId:  "$UnitDetails.courseId" }, "unitTitle": "$UnitDetails.title"  } }, {$lookup:  {"from": "Course2", "localField": "courseObjectId", "foreignField": "_id", as:  "Course2Details"  }  }, {$unwind: "$Course2Details"}, {$project: {_id: 0, "Boking_id": "$_id",  "Unit_title":  "$unitTitle", "course_title": "$Course2Details.title" }  }    ]).pretty()
    {
        "Boking_id" : ObjectId("5dff6c2a187da4c2b5ffc5a3"),
        "Unit_title" : "Unit title 1",
        "course_title" : "Title of the Course collection 1"
    }
    > 
    

    您正在寻找的解决方案是通过在聚合管道中使用多时间
    $lookup
    管道来解决的。e、 g:

    db.Bookings.aggregate([
        { 
            "$lookup" : 
                {"from":  "Unit", "localField": "unitId", "foreignField": "_id", as : "UnitDetails"   } 
        }, 
        {
            "$unwind" : "$UnitDetails"
        },
        {
            $lookup:   
                { "from": "Course", "localField": "UnitDetails.courseId", "foreignField": "_id", as: "CouserDetails"    }  
        },
        {
            "$unwind" : "$CouserDetails"
        },
        {   
            $project:  {_id:0, "Boking_id" : "$_id",  "Unit_title": "$UnitDetails.title", "course_title": "$CouserDetails.title" } 
        }   
    
    ]).pretty()
    
    上述查询的输出如下:

    {
        "Boking_id" : ObjectId("5dff07e4187da4c2b5ffc59f"),
        "Unit_title" : "unit title for course title 3",
        "course_title" : "Title of the Course collection 3"
    }
    
    > db.Bookings2.find()
    { "_id" : ObjectId("5dff6c2a187da4c2b5ffc5a3"), "unitId" : "5dff66c9187da4c2b5ffc5a2" }
    > db.Unit2.find()
    { "_id" : ObjectId("5dff66c9187da4c2b5ffc5a2"), "title" : "Unit title 1", "courseId" : "5dff6694187da4c2b5ffc5a0" }
    > db.Course2.find()
    { "_id" : ObjectId("5dff6694187da4c2b5ffc5a0"), "title" : "Title of the Course collection 1" }
    { "_id" : ObjectId("5dff6694187da4c2b5ffc5a1"), "title" : "Title of the Course collection 2" }
    > 
    > db.Bookings2.aggregate([ {$project: {"unitObjectId" : {$toObjectId: "$unitId"}} },  {$lookup:  {"from": "Unit2", "localField": "unitObjectId", "foreignField": "_id", as:  "UnitDetails" }  }, {$unwind: "$UnitDetails"}, {$project: {"courseObjectId":   {$toObjectId:  "$UnitDetails.courseId" }, "unitTitle": "$UnitDetails.title"  } }, {$lookup:  {"from": "Course2", "localField": "courseObjectId", "foreignField": "_id", as:  "Course2Details"  }  }, {$unwind: "$Course2Details"}, {$project: {_id: 0, "Boking_id": "$_id",  "Unit_title":  "$unitTitle", "course_title": "$Course2Details.title" }  }    ]).pretty()
    {
        "Boking_id" : ObjectId("5dff6c2a187da4c2b5ffc5a3"),
        "Unit_title" : "Unit title 1",
        "course_title" : "Title of the Course collection 1"
    }
    > 
    
    在所有三个集合中,我的初始集合数据集如下所示:

    > db.Bookings.find()
    { "_id" : ObjectId("5dff07e4187da4c2b5ffc59f"), "unitId" : ObjectId("5dff06d6187da4c2b5ffc59e") }
    > db.Unit.find()
    { "_id" : ObjectId("5dff06d6187da4c2b5ffc59e"), "title" : "unit title for course title 3", "courseId" : ObjectId("5dff010d187da4c2b5ffc59b") }
    > db.Course.find()
    { "_id" : ObjectId("5dff0108187da4c2b5ffc599"), "title" : "Title of the Course collection" }
    { "_id" : ObjectId("5dff010b187da4c2b5ffc59a"), "title" : "Title of the Course collection 2" }
    { "_id" : ObjectId("5dff010d187da4c2b5ffc59b"), "title" : "Title of the Course collection 3" }
    { "_id" : ObjectId("5dff010f187da4c2b5ffc59c"), "title" : "Title of the Course collection 4" }
    > 
    
    有关详细信息,请参阅以下参考资料:

  • 如果Unit&Course集合中unitId和courseId的值存储为简单字符串,则必须使用。我创建了另一组集合,并使用了
    $toObjectId
    ,如下所示:

    {
        "Boking_id" : ObjectId("5dff07e4187da4c2b5ffc59f"),
        "Unit_title" : "unit title for course title 3",
        "course_title" : "Title of the Course collection 3"
    }
    
    > db.Bookings2.find()
    { "_id" : ObjectId("5dff6c2a187da4c2b5ffc5a3"), "unitId" : "5dff66c9187da4c2b5ffc5a2" }
    > db.Unit2.find()
    { "_id" : ObjectId("5dff66c9187da4c2b5ffc5a2"), "title" : "Unit title 1", "courseId" : "5dff6694187da4c2b5ffc5a0" }
    > db.Course2.find()
    { "_id" : ObjectId("5dff6694187da4c2b5ffc5a0"), "title" : "Title of the Course collection 1" }
    { "_id" : ObjectId("5dff6694187da4c2b5ffc5a1"), "title" : "Title of the Course collection 2" }
    > 
    > db.Bookings2.aggregate([ {$project: {"unitObjectId" : {$toObjectId: "$unitId"}} },  {$lookup:  {"from": "Unit2", "localField": "unitObjectId", "foreignField": "_id", as:  "UnitDetails" }  }, {$unwind: "$UnitDetails"}, {$project: {"courseObjectId":   {$toObjectId:  "$UnitDetails.courseId" }, "unitTitle": "$UnitDetails.title"  } }, {$lookup:  {"from": "Course2", "localField": "courseObjectId", "foreignField": "_id", as:  "Course2Details"  }  }, {$unwind: "$Course2Details"}, {$project: {_id: 0, "Boking_id": "$_id",  "Unit_title":  "$unitTitle", "course_title": "$Course2Details.title" }  }    ]).pretty()
    {
        "Boking_id" : ObjectId("5dff6c2a187da4c2b5ffc5a3"),
        "Unit_title" : "Unit title 1",
        "course_title" : "Title of the Course collection 1"
    }
    > 
    

    @NEET,注意单元集合
    courseId
    的ObjectId值,而不仅仅是字符串,这就是为什么我提供了引用。如果您有简单字符串,请按照引用解决问题。单元中的courseId是字符串,但它当然是指_idcollection@NEETJASSI上述答案是否解决了您的问题?@NEETJASSI考虑了单位中courseId的字符串值,并更新了以前的答案,希望此查询能够解决您的问题。@NEET,请注意单位集合
    courseId
    的ObjectId值,而不仅仅是字符串,这就是为什么我提供了引用,如果你有简单的字符串,那么按照引用来解决这个问题。单位中的courseId是字符串,但它对_id的引用是collection@NEETJASSI上述答案是否解决了您的问题?@NEETJASSI考虑了courseId的字符串值(单位),并更新了之前的答案,希望这个问题能解决你的问题。