Mongodb Mongo展开子对象和过滤器
关于这个问题 今天,我有两个嵌套的对象数组: 我有一系列的会议,里面有一系列的邀请 我只需要投影会议,但是使用PartnerId过滤的邀请。所以我有很多邀请给不同的合作伙伴,但是当每个合作伙伴登录到系统时,他只能看到自己的邀请,而不能看到其他的邀请。换句话说,数组邀请将只有对其PartnerId的邀请 我可以进入项目部分,但是过滤掉了我丢失的邀请。我应该使用$filter还是$group来创建另一个数组 以下是我到目前为止得到的信息: 回答是我做的Mongodb Mongo展开子对象和过滤器,mongodb,mongodb-query,Mongodb,Mongodb Query,关于这个问题 今天,我有两个嵌套的对象数组: 我有一系列的会议,里面有一系列的邀请 我只需要投影会议,但是使用PartnerId过滤的邀请。所以我有很多邀请给不同的合作伙伴,但是当每个合作伙伴登录到系统时,他只能看到自己的邀请,而不能看到其他的邀请。换句话说,数组邀请将只有对其PartnerId的邀请 我可以进入项目部分,但是过滤掉了我丢失的邀请。我应该使用$filter还是$group来创建另一个数组 以下是我到目前为止得到的信息: 回答是我做的 db.getCollection(&quo
db.getCollection("ClientProject").aggregate(
[
{
"$match" : {
"$and" : [
{
"PartnerIds" : {
"$in" : [
"5f9b247f0ca60a000232cacf"
]
}
},
{
"$nor" : [
{
"Meetings" : {
"$exists" : false
}
},
{
"Meetings" : {
"$size" : 0.0
}
},
{
"Meetings" : {
"$eq" : null
}
}
]
}
]
}
},
{
"$unwind" : {
"path" : "$Meetings"
}
},
{
"$project" : {
"Meetings" : "$Meetings"
}
},
{
"$replaceRoot" : {
"newRoot" : "$Meetings"
}
},
{
"$addFields" : {
"Invites" : {
"$filter" : {
"input" : "$Invites",
"as" : "invite",
"cond" : {
"$eq" : [
"$$invite.PartnerId",
"5f9b247f0ca60a000232cacf"
]
}
}
}
}
}
],
{
"allowDiskUse" : false
}
);
使用
$filter
是实现目标的一个好方法,以下是实现这一目标的方法:
db.collection.aggregate([
{
“$match”:{
“$nor”:[
{
“会议”:{
“$exists”:false
}
},
{
“会议”:{
“$size”:0.0
}
}
]
}
},
{
“$REWIND”:{
“路径”:“$Meetings”
}
},
{
“$project”:{
“邀请”:{
“$filter”:{
“输入”:“$Meetings.invests”,
“作为”:“邀请”,
“条件”:{
“$eq”:[
“$$invite.PartnerName”,
“蒂亚戈公园”
]
}
}
}
}
}
])
但如果这样做,我会过滤并发出邀请,我需要过滤并在有会议的地方发出邀请。
db.getCollection("ClientProject").aggregate(
[
{
"$match" : {
"$and" : [
{
"PartnerIds" : {
"$in" : [
"5f9b247f0ca60a000232cacf"
]
}
},
{
"$nor" : [
{
"Meetings" : {
"$exists" : false
}
},
{
"Meetings" : {
"$size" : 0.0
}
},
{
"Meetings" : {
"$eq" : null
}
}
]
}
]
}
},
{
"$unwind" : {
"path" : "$Meetings"
}
},
{
"$project" : {
"Meetings" : "$Meetings"
}
},
{
"$replaceRoot" : {
"newRoot" : "$Meetings"
}
},
{
"$addFields" : {
"Invites" : {
"$filter" : {
"input" : "$Invites",
"as" : "invite",
"cond" : {
"$eq" : [
"$$invite.PartnerId",
"5f9b247f0ca60a000232cacf"
]
}
}
}
}
}
],
{
"allowDiskUse" : false
}
);