[英]MongoDB aggregate different collections
從昨天開始,我一直在嘗試進行這種聚合,但沒有成功,希望你們能給我一些想法。 一點背景,我有 2 個 collections,一個用於結果,另一個用於問題。 結果基本上是人們解決問題的地方,所以如果我沒記錯的話,它可以有 1 個問題或最多 99 個問題。 這是簡化的架構:
Results Schema:
_id: ObjectId("6010664ac5c4f77f26f5d005")
questions: [
{
_id: ObjectId("5d2627c94bb703bfcc910763"),
correct: false
},
{
_id: ObjectId("5d2627c94bb703bfcc910764"),
correct: true
},
{
_id: ObjectId("5d2627c94bb703bfcc910765"),
correct: true
}
]
因此,對於這個特定的 object,用戶回答了 3 個問題並答對了其中 2 個。
Questions Schema:
_id: ObjectId("5d2627c94bb703bfcc910763")
我正在努力做的是:對於所有問題模式中的每個元素,我必須檢查問題是否已得到回答 - 即(檢查問題模式中是否有問題數組 == _id 的 _id,是的,可以_id 與 Questions Schema 相同的多個問題,但 Questions Schema _id 是唯一的)。 如果回答了該問題,我需要檢查是否正確 = 真,如果是,我將其添加到 correctAnswer 變量,如果不是,則添加 wrongAnswer。
到目前為止,我已經嘗試了很多有條件的事情,分組以獲得正確和錯誤答案的 $sum,查找以加入 collections 但到目前為止,我什至無法只顯示聚合結果。
我嘗試過的其中一件事(我試圖先做嬰兒步驟)但如前所述,甚至無法打印結果。
Result.aggregate([
$lookup: {
from: 'question',
localField: 'questions._id',
foreignField: '_id',
as: 'same'
},
但這讓我得到了 collections 組合,並且“相同”作為空數組出現,嘗試使用匹配也沒有運氣。 我還做了一個 $project 來獲取我想要的信息
$project: {
_id: 0,
questions: {
_id: 1,
correct: 1
}
},
嘗試使用 $group: $group: { _id: "$_id", $cond: {if: {"$correct": { $eq: true}}, then: {testField: {$sum: 1}}, else : {testField: 0}} } 正如我所說,我只是想做一些小步驟,所以 testField 是手動設置的,還嘗試了 stackoverflow 中的許多其他東西。 感謝您的幫助,對於很長的文字感到抱歉,只是想舉一些我做過和嘗試過的例子。
TLDR :需要從Results Schema
中找到一個question
,其中_id
與Questions Schema
中的_id
匹配,如果有,請檢查correct: true
或correct: false
。 根據 Questions Schema 中的每個question
,相應地更新Questions Schema
Questions Schema
多少是正確的,有多少是錯誤的。 示例: newField: {correctAnswer: 4, wrongAnswer: 3}
所以在這種情況下, Result schema
question
數組中有 7 個問題與Question Schema
中的_id
匹配,4 個correct: true
,3 個correct: false
。 然后對於Question Schema
的 rest 這樣繼續下去
我不知道同時"$lookup"
和update
的方法。 可能有更好的方法來執行此操作,但下面的聚合管道會創建可在后續update
中使用的文檔。 管道通過單個Result
_id
正確地計算重復問題,以防有人不斷嘗試一個問題直到他們做對為止。 一個可能的問題是,如果問題沒有Result
答案,則不會創建"newField": { "correctAnswers": 0, "wrongAnswers": 0 }
文檔。
db.Question.aggregate([
{
// lookup documents in Result that match _id
"$lookup": {
"from": "Result",
"localField": "_id",
"foreignField": "questions._id",
"as": "results"
}
},
{
// unwind everything
"$unwind": "$results"
},
{
// more of everything
"$unwind": "$results.questions"
},
{
// only keep answers that match question
"$match": {
"$expr": { "$eq": [ "$_id", "$results.questions._id" ] }
}
},
{
// reassemble and count correct/wrong answers
"$group": {
"_id": "$_id",
"correct": {
"$sum": {
"$cond": [ { "$eq": [ "$results.questions.correct", true ] }, 1, 0 ]
}
},
"wrong": {
"$sum": {
"$cond": [ { "$eq": [ "$results.questions.correct", false ] }, 1, 0 ]
}
}
}
},
{
// project what you want as output
"$project": {
newField: {
correctAnswers: "$correct",
wrongAnswers: "$wrong"
}
}
}
])
在mongoplayground.net上試用。
對於因為Question
集合在不同的數據庫中而無法使用"$lookup"
的場景,可以使用Result
集合生成output個文檔來更新Question
集合。
這是一種方法。
db.Result.aggregate([
{
"$unwind": "$questions"
},
{
"$group": {
"_id": "$questions._id",
"correct": {
"$push": "$questions.correct"
}
}
},
{
"$project": {
"newField": {
"correctAnswers": {
"$size": {
"$filter": {
"input": "$correct",
"as": "bool",
"cond": "$$bool"
}
}
},
"wrongAnswers": {
"$size": {
"$filter": {
"input": "$correct",
"as": "bool",
"cond": { "$not": "$$bool" }
}
}
}
}
}
}
])
在mongoplayground.net上試用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.