MongoDB条件关系查询
MongoDB 4.2.2 我有两个集合投票和投票,我需要从“投票”集合中抓取字段MongoDB条件关系查询,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,MongoDB 4.2.2 我有两个集合投票和投票,我需要从“投票”集合中抓取字段选民选择,并将其添加到投票中,但前提是某些条件匹配投票id&选民id 用户: 民意测验: { "_id" : ObjectId("5f87d988ddae726a3dbe5459"), "name" : "RedVsWhite" }, { "_id" : ObjectId("5f51408ffc1d0437
选民选择
,并将其添加到投票中,但前提是某些条件匹配投票id
&选民id
用户:
民意测验:
{
"_id" : ObjectId("5f87d988ddae726a3dbe5459"),
"name" : "RedVsWhite"
},
{
"_id" : ObjectId("5f51408ffc1d0437fa31d6f7"),
"name" : "ApplesVsOrange",
"total_votes" : "0",
}
投票:
{
"_id" : ObjectId("5f864addddae726a3dbe53de"),
"voter_id" : ObjectId("5f867e0d126ddbde24d6ee73"), //this is the id of the person
"poll_id" : ObjectId("5f87d988ddae726a3dbe5459"), //this is the poll id
"voter_selection" : "red"
}
我需要这个结果:
{
"_id" : ObjectId("5f87d988ddae726a3dbe5459"),
"name" : "RedVsWhite",
"total_votes" : "0",
"voter_selection" : red // show this if it finds votes with this _id and also if user_id match & voter_id
},
{
"_id" : ObjectId("5f51408ffc1d0437fa31d6f7"),
"name" : "ApplesVsOrange",
"total_votes" : "0"
/// dont show voter_selection if nothing match with voter_id and poll_id
}
这个查询可以工作,但问题是如果没有匹配项,它将不会显示轮询行,我需要的是即使没有匹配项也获取所有轮询,但如果有匹配项,则将字段选民选择添加到行中
db.getCollection("Polls").aggregate(
[
{
"$lookup": {
"localField": "_id",
"from": "VotesRecords",
"foreignField": "poll_id",
"as": "VotesRecords"
}
},
{
"$unwind": {
"path": "$VotesRecords",
"preserveNullAndEmptyArrays": true
}
},
{
"$match": {
"VotesRecords.voter_id": ObjectId("5f867e0d126ddbde24d6ee73")
}
},
{
"$project": {
"_id": "$_id",
"name": "$name",
"voter_selection": "$VotesRecords.voter_selection"
}
}
]
);
$group
按null键,并将根的所有元素组成一个数组
$lookup
与VotesRecords
加入集合并获取用户详细信息
$unwind
解构VotesRecords
数组
$project
在根数组上迭代循环$map
,检查条件是否匹配轮询id
,然后使用
$unwind
解构根数组
$replaceWith
替换根目录中的对象
第二种方法,您可以在管道之后添加以下更改
$project
显示投票者选择
如果投票者id
匹配,否则$$REMOVE
$group
通过\u id
并使用$mergeObjects
$replaceWith
替换根对象
//聚合看起来是正确的。您只是缺少了“total_Voces”:“$total_Voces”在您的$project阶段。$total_Voces只是民意调查收集的一个示例
db.getCollection("Polls").aggregate(
[
{
"$lookup": {
"localField": "_id",
"from": "VotesRecords",
"foreignField": "poll_id",
"as": "VotesRecords"
}
},
{
"$unwind": {
"path": "$VotesRecords",
"preserveNullAndEmptyArrays": true
}
},
{
"$match": {
"VotesRecords.voter_id": ObjectId("5f867e0d126ddbde24d6ee73")
}
},
{
"$project": {
"_id": "$_id",
"name": "$name",
"voter_selection": "$VotesRecords.voter_selection"
}
}
]
);
db.getCollection("Polls").aggregate([
{
$group: {
_id: null,
root: { $push: "$$ROOT" }
}
},
{
$lookup: {
from: "VotesRecords",
as: "VotesRecords",
pipeline: [{ $match: { voter_id: ObjectId("5f867e0d126ddbde24d6ee73") } }]
}
},
{
"$unwind": {
"path": "$VotesRecords",
"preserveNullAndEmptyArrays": true
}
},
{
$project: {
root: {
$map: {
input: "$root",
in: {
$mergeObjects: [
"$$this",
{
$cond: [
{ $eq: ["$$this._id", "$VotesRecords.poll_id"] },
{ voter_selection: "$VotesRecords.voter_selection" },
{}
]
}
]
}
}
}
}
},
{ $unwind: "$root" },
{ $replaceWith: "$root" }
])
// <= skipping your pipelines here
{
"$project": {
"_id": "$_id",
"name": "$name",
"total_votes": 1,
"voter_selection": {
$cond: [
{ $eq: ["$VotesRecords.voter_id", ObjectId("5f867e0d126ddbde24d6ee73")] },
"$VotesRecords.voter_selection",
"$$REMOVE"
]
}
}
},
{
$group: {
_id: "$_id",
root: { $mergeObjects: "$$ROOT" }
}
},
{ $replaceWith: "$root" }