我有这样的集合数据格式, 我现在传输和_id和行。如何返回256个元素数组 例如:发送_id:4行1045,返回行(1045-256)到1045
{
"_id" : 1,
"fingerprint" : [
{
"line" : 1,
"sub_finger" : "00000000"
},
{
"line" : 2,
"sub_finger" : "00000000"
},
{
"line" : 3,
"sub_finger" : "00000000"
},
{
"line" : 4,
"sub_finger" : "00000000"
},
{
"line" : 5,
"sub_finger" : "00000000"
}, ...
答案 0 :(得分:1)
您可以在mongodb java驱动程序中使用以下代码。
MongoClient client = new MongoClient("localhost", 27017);
MongoDatabase database = client.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("collection");
Document result = collection.find(Filters.eq("_id", 4)).projection(Projections.slice("fingerprint", 1045-256, 256)).first()
答案 1 :(得分:0)
在mongodb聚合管道中使用$unwind,$match,$sort,$skip和$limit,我们可以实现此目标
MongoDB shell Query
db.mycollection.aggregate([{$ unwind:&#34; $ fingerprint&#34;},{$ match:{_ id:4}},{$ sort:{line:1}},{$ skip :1045},{$ limit:256}]);
说明 - 此查询的工作原理
$ unwind - 将指纹数据展开为单个文档
$ match - 匹配所需的文档并将其移至聚合管道中的下一级别(过滤集合并帮助更快地交付)
$ sort - 按升序对未展开的文档进行排序(我们需要这个,否则我们可能会错过很少使用Skip和限制的文档)
$ skip - 需要跳过我们已经呈现的文件
$ limit - 限制结果的大小
实施例
让我们有一个包含3条记录的集合
db.mycollection.find()
[{
"_id" : 1,
"fingerprint" : [
{
"line" : 1,
"sub_finger" : "00000000"
},
{
"line" : 2,
"sub_finger" : "00000000"
},
{
"line" : 3,
"sub_finger" : "00000000"
},
{
"line" : 4,
"sub_finger" : "00000000"
},
{
"line" : 5,
"sub_finger" : "00000000"
}]
},
{
"_id" : 2,
"fingerprint" : [
{
"line" : 1,
"sub_finger" : "00000000"
},
{
"line" : 2,
"sub_finger" : "00000000"
},
{
"line" : 3,
"sub_finger" : "00000000"
},
{
"line" : 4,
"sub_finger" : "00000000"
},
{
"line" : 5,
"sub_finger" : "00000000"
}]
},
{
"_id" : 3,
"fingerprint" : [
{
"line" : 1,
"sub_finger" : "00000000"
},
{
"line" : 2,
"sub_finger" : "00000000"
},
{
"line" : 3,
"sub_finger" : "00000000"
},
{
"line" : 4,
"sub_finger" : "00000000"
},
{
"line" : 5,
"sub_finger" : "00000000"
}]
}]
让我们使用聚合管道来获得所需的结果
db.mycollection.aggregate([{$unwind:"$fingerprint"}])
{ "_id" : 1, "fingerprint" : { "line" : 1, "sub_finger" : "00000000" } }
{ "_id" : 1, "fingerprint" : { "line" : 2, "sub_finger" : "00000000" } }
{ "_id" : 1, "fingerprint" : { "line" : 3, "sub_finger" : "00000000" } }
{ "_id" : 1, "fingerprint" : { "line" : 4, "sub_finger" : "00000000" } }
{ "_id" : 1, "fingerprint" : { "line" : 5, "sub_finger" : "00000000" } }
{ "_id" : 2, "fingerprint" : { "line" : 1, "sub_finger" : "00000000" } }
{ "_id" : 2, "fingerprint" : { "line" : 2, "sub_finger" : "00000000" } }
{ "_id" : 2, "fingerprint" : { "line" : 3, "sub_finger" : "00000000" } }
{ "_id" : 2, "fingerprint" : { "line" : 4, "sub_finger" : "00000000" } }
{ "_id" : 2, "fingerprint" : { "line" : 5, "sub_finger" : "00000000" } }
{ "_id" : 3, "fingerprint" : { "line" : 1, "sub_finger" : "00000000" } }
{ "_id" : 3, "fingerprint" : { "line" : 2, "sub_finger" : "00000000" } }
{ "_id" : 3, "fingerprint" : { "line" : 3, "sub_finger" : "00000000" } }
{ "_id" : 3, "fingerprint" : { "line" : 4, "sub_finger" : "00000000" } }
{ "_id" : 3, "fingerprint" : { "line" : 5, "sub_finger" : "00000000" } }
$ unwind - 展开集合,现在我们有15个文件
db.mycollection.aggregate([{$unwind:"$fingerprint"}, {$match: {_id:1}}])
Note: Pass the respective document id in the $match query
{ "_id" : 1, "fingerprint" : { "line" : 1, "sub_finger" : "00000000" } }
{ "_id" : 1, "fingerprint" : { "line" : 2, "sub_finger" : "00000000" } }
{ "_id" : 1, "fingerprint" : { "line" : 3, "sub_finger" : "00000000" } }
{ "_id" : 1, "fingerprint" : { "line" : 4, "sub_finger" : "00000000" } }
{ "_id" : 1, "fingerprint" : { "line" : 5, "sub_finger" : "00000000" } }
$ skip - 要删除文档 $ limit - 限制最终结果
db.mycollection.aggregate([{$unwind:"$fingerprint"}, {$match: {_id:1}},{$sort: {line:1}}, {$skip:1}, {$limit:2}]);
{ "_id" : 1, "fingerprint" : { "line" : 2, "sub_finger" : "00000000" } }
{ "_id" : 1, "fingerprint" : { "line" : 3, "sub_finger" : "00000000" } }
请注意,我已跳过1个文档并将结果限制为2个文档
希望它有帮助!