使用对象ID

时间:2018-01-18 10:40:03

标签: mongodb mongodb-query aggregation-framework

有没有更好的方法从collection2中基于具有object1中的对象id的数组字段检索值?我试图使用$ project但未能获得所有必填字段

要聚合的集合:

collection1:

{
    "_id" : ObjectId("5a58910de202796cfef41c6a"),
    "sortOrder" : 5,
    "title" : "Question 1 ?",
    "freeTextIncluded" : false,
    "freeText" : false,
    "resultChart" : "pieChart",
    "answer" : [ 
        ObjectId("5a579fefd5554706b446cc71"), 
        ObjectId("5a587f17e4b2de0d683f96a4"), 
        ObjectId("5a587f20e4b2de0d683f96a5"), 
        ObjectId("5a587f29e4b2de0d683f96a6")
    ],
    "state" : "active",
    "__v" : 1,
    "description" : ""
}

collection2:

{
    "_id" : ObjectId("5a579fefd5554706b446cc71"),
    "slug" : "answer-1",
    "title" : "Answer 1",
    "state" : "active",
    "__v" : 0,
    "author" : ObjectId("5a2e6b56e593c8525ced34b8"),
    "body" : "<p>Lipsum...</p>"
}

{
    "_id" : ObjectId("5a587f17e4b2de0d683f96a4"),
    "slug" : "answer-2",
    "title" : "Answer 2",
    "state" : "active",
    "__v" : 0,
    "body" : ""
}

{
    "_id" : ObjectId("5a587f20e4b2de0d683f96a5"),
    "slug" : "answer-3",
    "title" : "Answer 3",
    "state" : "active",
    "__v" : 0,
    "body" : "",
    "isCorrect" : true,
    "sortOrder" : 3
}

{
    "_id" : ObjectId("5a587f29e4b2de0d683f96a6"),
    "slug" : "answer-4",
    "title" : "Answer 4",
    "state" : "active",
    "__v" : 0,
    "body" : ""
}

这种聚合工作正常,但我只是想知道是否有更好/更短的方式来聚合2个集合...

db.getCollection('questions').aggregate([
{
    $match: {'_id': ObjectId('5a58910de202796cfef41c6a')  }
},
{
    $unwind: "$answer"
},
{
    $lookup:
    {
       from: "answers",
       localField: "answer",
       foreignField: "_id",
       as: "answers"
   }
},
{
      $match: { "answers": { $ne: [] }}
},
{
    $unwind: "$answers"
},
{
       $group: {
           _id : ObjectId('5a58910de202796cfef41c6a'),
           answerList: {$push: "$answers"},
           title: {$first: "$title"},
           sortOrder: {$first: "$sortOrder"},
           description: {$first: "$description"},
           resultChart: {$first: "$resultChart"},
           freeTextIncluded: {$first: "$freeTextIncluded"},
       }
}
]);

1 个答案:

答案 0 :(得分:0)

您需要像这样改进查询:

db.getCollection('test').aggregate([{
    $match: {
        '_id': ObjectId('5a58910de202796cfef41c6a')
    }
},
{
    $lookup: {
        from: "answers",
        localField: "answer",
        foreignField: "_id",
        as: "answers"
    }
},
{
    $unwind: {
        path: "$answers",
        preserveNullAndEmptyArrays: true
    }
},
{
    $group: {
        _id: ObjectId('5a58910de202796cfef41c6a'),
        answerList: {
            $push: "$answers"
        },
        title: {
            $first: "$title"
        },
        sortOrder: {
            $first: "$sortOrder"
        },
        description: {
            $first: "$description"
        },
        resultChart: {
            $first: "$resultChart"
        },
        freeTextIncluded: {
            $first: "$freeTextIncluded"
        },
    }
}])