Mongodb 查找公共元素bw pipeline1和pipeline2
我的文件-Mongodb 查找公共元素bw pipeline1和pipeline2,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我的文件- { "_id" : ObjectId("5b654eecb5973f0a7945d1fa"), "userId" : "5b4098790be60f1c2b4c9b4c", "creationTime":ISODate("2018-07-30T12:04:32.951+0000") "event" : "Add to Cart", "attributes" : { "Amount" : "1000", "Quantity" : "1", "Ca
{
"_id" : ObjectId("5b654eecb5973f0a7945d1fa"),
"userId" : "5b4098790be60f1c2b4c9b4c",
"creationTime":ISODate("2018-07-30T12:04:32.951+0000")
"event" : "Add to Cart",
"attributes" : {
"Amount" : "1000",
"Quantity" : "1",
"Category" : "shoes",
"Product" : "shoes"
}
}
,
{
"_id" : ObjectId("5b654eecb5973f0a7945d1fb"),
"userId" : "5b4098790be60f1c2b4c9b4c",
"creationTime":ISODate("2018-08-05T12:04:32.951+0000")
"event" : "Item Viewed",
"attributes" : {
"item" : "laptop",
"color" : "black",
"size" : "small"
}
}
,
{
"_id" : ObjectId("5b654eecb5973f0a7945d1fb"),
"userId" : "5b58463e0be60f139e67cfb9",
"creationTime":ISODate("2018-07-25T12:04:32.951+0000")
"event" : "Item Viewed",
"attributes" : {
"item" : "laptop",
"color" : "black",
"size" : "small"
}
}
我想要
执行这两个事件的用户ID列表
添加到购物车,其中creationTime在ISODate之前(“2018-08-04T12:04:32.951+0000”)金额=1000,产品为鞋子
及
项目查看事件,其中creationTime在ISODate之前(“2018-08-04T12:04:32.951+0000”),项目为笔记本电脑,颜色为黑色
我正在使用facet来获得结果
我的问题-
db.getCollection("3_event").aggregate([
{$facet:{
"pipeline1":[
{$match:{"name":"Add to Cart","creationTime":{$lt:new ISODate("2018-
08-
04T12:00:00Z")},"attributes.Category":"shoes",
"attributes.Product":"shoes"}},
{$group:{"_id":"$userId",count:{$sum:1}}},
{$match:{count:{$gt:1}}},
{$project:{"pipeline1._id":1}}
],
"pipeline2":[
{$match:{"name":"Item Viewed","creationTime":{$lt:new ISODate("2018-
08-04T12:00:00Z")},"attributes.item":"laptop",
"attributes.color":"black"}},
{$group:{"_id":"$userId",count:{$sum:1}}},
{$match:{count:{$gt:0}}},
{$project:{"pipeline2._id":1}}
]
}},
{$project:{"result":
{$concatArrays:["$pipeline1","$pipeline2"]}
}},
{$unwind:"$result"},
{$replaceRoot:{newRoot:"$result"}},
{$group:{_id:"$_id",count:{$sum:1}}},
{$match:{count:{$gte:2}}}]); Output is-
{
"_id" : "5b4098790be60f1c2b4c9b4c",
"count" : 2.0
}
使用而不是
$concatarray
基本上我想要在两个管道数组中都通用的id。setUnion只给我们唯一的元素。所以如何知道在两个管道数组中哪个id是通用的。在mu查询中,我使用group by查找公共id。如果您的id
相同,那么您的文档也将相同,那么它可能会工作。。。或者请发布您的实际样本数据…您可以将您的方面查询添加到帖子中吗?