嗨,我有一个userAnswer schema
,看起来像这样
var userAnswer = mongoose.Schema({
'questionId' : { 'type' : mongoose.Schema.Types.ObjectId, 'ref': 'Question'},
'userId' : { 'type': mongoose.Schema.Types.ObjectId, 'ref' : 'User'},
'score' : 1
})
然后是一个userAttempt schema
,其中包含与此问题有关的尝试
var userAttempt = mongoose.Schema({
'userAnswerId' : { 'type' : mongoose.Schema.Types.ObjectId, 'ref' : 'UserAnswer'},
'attemptData' : [{
'userInputMCQ' : String,
'time' : Date
}],
})
最后一个usersnapshots schema
保存有关attemptData
的{{1}}字段中每个元素的快照
看起来像这样
userAttempt Schema
现在,我想以var userAttemptSnapshot= mongoose.Schema({
userAttemptDataId : {type: mongoose.Schema.Types.ObjectId },
count : {type: Number, default: 1},
userData :[ {
value: String,
timeOfCreation : { type : Date, default : Date.now }
}]
},
{ timestamps: true }
);
字段data
中的所有question
的方式提取与documents
有关的所有attempData
UserAttempt Schema
使用 'attemptData' : [{
'userInputMCQ' : String,
'time' : Date
}],
作为单独的attempt
和其各自的snapshots
来获取
我的aggregation
是
aggregation query
编辑:
我的db.useranswers.aggregate([
{ $match : { 'questionId' : ObjectId("5a2dfefd6dc3de029e488961"),'userId' :ObjectId("5a2cea6d6dc3de029e488814") }},
{ '$lookup' : {from: "userattempts",
localField: "_id",
foreignField: "userAnswerId",
as: "attempts"}},
{ '$unwind' : '$attempts'},
{'$unwind' : '$attempts.attemptData'}
//after this stage i will use lookup to get snapshot data but i dont get those attempts as individual documents so that i could use lookup here
})
收藏是
userAnswer
{
"_id":ObjectId("5b4f359244781a45bcc98fcb") ,
"userId":ObjectId("5a2cea6d6dc3de029e488814"),
"questionId": ObjectId("5a631937e2509a3288171c37"),
}
集合是
UserAttempt
我想要这样的输出
{
"_id":ObjectId("5b4f359204a7251ebcabef24"),
"attemptData":[
{
"_id" : ObjectId("5b4f359204a7251ebcabef23"),
"userInputMCQ" : "0"
"timeOfCreation" : ISODate("2018-07-18T18:11:54.855+05:30"),
},
{
"_id" : ObjectId("5b4f35bb04a7251ebcabef27"),
"userInputMCQ" : "0",
"timeOfCreation" : ISODate("2018-07-18T18:12:35.765+05:30"),
} ]
}
UserAttemptSnapshot collection is
{
"_id":ObjectId("5b4f359204a7251ebcabef25"),
"userAttemptId":ObjectId("5b4f359204a7251ebcabef23"),
"count" : 2,
"userData" : [ {
'value' : "Some String" ,
"_id":ObjectId("5b4f359204a7251ebcabef26")
},
{
'value' : "Some String" ,
"_id": ObjectId("5b4f359204a7251ebcabef27")
} ]
},
{
"_id":ObjectId("5b4f359204a7251ebcabef25"),
"userAttemptId":ObjectId("5b4f359204a7251ebcabef27"),
"count" : 1,
"userData" : [ {
'value' : "Some String" ,
"_id":ObjectId("5b4f359204a7251ebcabeaee")
},
]
}
答案 0 :(得分:0)
您可以在3.6中使用以下汇总。
db.useranswers.aggregate([
{"$lookup":{
"from": "userattempts",
"let": {"id":"$_id"},
"pipeline":[
{"$match":{"$expr":{"$eq":["$$id","$userAnswerId"]}}},
{"$unwind":"$attemptData"},
{"$lookup":{
"from": "userattemptsnapshots",
"localField": "attemptData._id",
"foreignField":"userAttemptId",
"as": "attemptData.attemptsnapshot"
}},
{"$unwind":"$attemptData.attemptsnapshot"},
{"$group":{
"_id":"$userAnswerId",
"attemptData":{
"$push":"$attemptData"
}
}}
],
"as": "attempts"
}},
{"$unwind":"$attempts"},
])
更新3.4版本
db.useranswers.aggregate([
{"$lookup":{
"from": "userattempts",
"localField": "_id",
"foreignField":"userAnswerId",
"as": "attempts"
}},
{"$unwind":"$attempts"},
{"$unwind":"$attempts.attemptData"},
{"$lookup":{
"from": "userattemptsnapshots",
"localField": "attempts.attemptData._id",
"foreignField":"userAttemptId",
"as": "attempts.attemptData.attemptsnapshot"
}},
{"$unwind":"$attempts.attemptData.attemptsnapshot"}
])