我在MongoDb中有一个查询,我使用$in
从一些集合中获取记录,我在这里传递一个数组。查询就像这样
db.getCollection('feed').find({"_id" : {"$in" : [3,5,8,64,23,18,78,67]}});
因为这个查询工作得很好。 它将获取这些ID的所有记录,但我只想获取每个Id的5条记录,例如Id 3的5条记录,ID为Id的每条ID的5条记录,以及每条ID的等等。< /强>
我甚至试过$last
但到目前为止没有成功。
db.getCollection('feed').find({"_id" : {"$in" : [3,5,8,64,23,18,78,67],"$last" : 5}});
有什么方法可以实现这个目标吗?
答案 0 :(得分:1)
使用聚合管道的方法之一如下所示
让我们有像
这样的文件{ "_id" : ObjectId("5a2a793118e947e4f4c1e9f1"), "myId" : 5 }
{ "_id" : ObjectId("5a2a793118e947e4f4c1e9f2"), "myId" : 5 }
{ "_id" : ObjectId("5a2a793118e947e4f4c1e9f3"), "myId" : 5 }
{ "_id" : ObjectId("5a2a793118e947e4f4c1e9f4"), "myId" : 5 }
{ "_id" : ObjectId("5a2a793118e947e4f4c1e9f5"), "myId" : 5 }
{ "_id" : ObjectId("5a2a793118e947e4f4c1e9f6"), "myId" : 5 }
{ "_id" : ObjectId("5a2a793118e947e4f4c1e9f7"), "myId" : 5 }
{ "_id" : ObjectId("5a2a793118e947e4f4c1e9f8"), "myId" : 5 }
{ "_id" : ObjectId("5a2a793118e947e4f4c1e9f9"), "myId" : 5 }
{ "_id" : ObjectId("5a2a793118e947e4f4c1e9fa"), "myId" : 5 }
{ "_id" : ObjectId("5a2a794918e947e4f4c1e9fb"), "myId" : 3 }
{ "_id" : ObjectId("5a2a794918e947e4f4c1e9fc"), "myId" : 3 }
{ "_id" : ObjectId("5a2a794918e947e4f4c1e9fd"), "myId" : 3 }
{ "_id" : ObjectId("5a2a794918e947e4f4c1e9fe"), "myId" : 3 }
{ "_id" : ObjectId("5a2a794918e947e4f4c1e9ff"), "myId" : 3 }
{ "_id" : ObjectId("5a2a794918e947e4f4c1ea00"), "myId" : 3 }
{ "_id" : ObjectId("5a2a794918e947e4f4c1ea01"), "myId" : 3 }
{ "_id" : ObjectId("5a2a794918e947e4f4c1ea02"), "myId" : 3 }
{ "_id" : ObjectId("5a2a794918e947e4f4c1ea03"), "myId" : 3 }
{ "_id" : ObjectId("5a2a794918e947e4f4c1ea04"), "myId" : 3 }
{ "_id" : ObjectId("5a2a795018e947e4f4c1ea05"), "myId" : 103 }
{ "_id" : ObjectId("5a2a795018e947e4f4c1ea06"), "myId" : 103 }
{ "_id" : ObjectId("5a2a795018e947e4f4c1ea07"), "myId" : 103 }
{ "_id" : ObjectId("5a2a795018e947e4f4c1ea08"), "myId" : 103 }
{ "_id" : ObjectId("5a2a795018e947e4f4c1ea09"), "myId" : 103 }
{ "_id" : ObjectId("5a2a795018e947e4f4c1ea0a"), "myId" : 103 }
{ "_id" : ObjectId("5a2a795018e947e4f4c1ea0b"), "myId" : 103 }
{ "_id" : ObjectId("5a2a795018e947e4f4c1ea0c"), "myId" : 103 }
{ "_id" : ObjectId("5a2a795018e947e4f4c1ea0d"), "myId" : 103 }
{ "_id" : ObjectId("5a2a795018e947e4f4c1ea0e"), "myId" : 103 }
{ "_id" : ObjectId("5a2a795e18e947e4f4c1ea19"), "myId" : 45 }
{ "_id" : ObjectId("5a2a795e18e947e4f4c1ea1a"), "myId" : 45 }
{ "_id" : ObjectId("5a2a795e18e947e4f4c1ea1b"), "myId" : 45 }
{ "_id" : ObjectId("5a2a795e18e947e4f4c1ea1c"), "myId" : 45 }
{ "_id" : ObjectId("5a2a795e18e947e4f4c1ea1d"), "myId" : 45 }
{ "_id" : ObjectId("5a2a795e18e947e4f4c1ea1e"), "myId" : 45 }
{ "_id" : ObjectId("5a2a795e18e947e4f4c1ea1f"), "myId" : 45 }
{ "_id" : ObjectId("5a2a795e18e947e4f4c1ea20"), "myId" : 45 }
{ "_id" : ObjectId("5a2a795e18e947e4f4c1ea21"), "myId" : 45 }
{ "_id" : ObjectId("5a2a795e18e947e4f4c1ea22"), "myId" : 45 }
上面显示的样本集有四个不同的id 3,5,45和103
让我们从id 3和5中选择5条记录
db.getCollection('feed').aggregate([
{
$facet: {
"result1": [
{$match:{ myId:3}},
{$limit: 5}
],
"result2": [
{$match: {"myId": 5}},
{$limit: 5}
]
}
},
{$project : { result: {$concatArrays:["$result1", "$result2"]}} },
{$unwind:"$result"}
])
执行此查询后,我们将得到如下所示的结果
{ "result" : { "_id" : ObjectId("5a2a794918e947e4f4c1e9fb"), "myId" : 3 } }
{ "result" : { "_id" : ObjectId("5a2a794918e947e4f4c1e9fc"), "myId" : 3 } }
{ "result" : { "_id" : ObjectId("5a2a794918e947e4f4c1e9fd"), "myId" : 3 } }
{ "result" : { "_id" : ObjectId("5a2a794918e947e4f4c1e9fe"), "myId" : 3 } }
{ "result" : { "_id" : ObjectId("5a2a794918e947e4f4c1e9ff"), "myId" : 3 } }
{ "result" : { "_id" : ObjectId("5a2a793118e947e4f4c1e9f1"), "myId" : 5 } }
{ "result" : { "_id" : ObjectId("5a2a793118e947e4f4c1e9f2"), "myId" : 5 } }
{ "result" : { "_id" : ObjectId("5a2a793118e947e4f4c1e9f3"), "myId" : 5 } }
{ "result" : { "_id" : ObjectId("5a2a793118e947e4f4c1e9f4"), "myId" : 5 } }
{ "result" : { "_id" : ObjectId("5a2a793118e947e4f4c1e9f5"), "myId" : 5 } }