Java 如何将256元素数组检索到mongodb?

Java 如何将256元素数组检索到mongodb?,java,mongodb,Java,Mongodb,我有这样的数据收集格式, 我现在传输和_id和线路。如何返回256元素数组 例如:传输id:4线1045,从(1045-256)到1045的返回线 在mongodb聚合管道中使用、和可以实现这一点 MongoDB shell查询 聚合([{$unwind:$fingerprint},{$match:{{u id:4},{$sort:{line:1},{$skip:1045},{$limit:256}]) 说明-此查询的工作原理 $unwind-将指纹数组展开为单个文档 db.mycollecti

我有这样的数据收集格式, 我现在传输和_id和线路。如何返回256元素数组 例如:传输id:4线1045,从(1045-256)到1045的返回线

在mongodb聚合管道中使用、和可以实现这一点

MongoDB shell查询

聚合([{$unwind:$fingerprint},{$match:{{u id:4},{$sort:{line:1},{$skip:1045},{$limit:256}])

说明-此查询的工作原理

$unwind-将指纹数组展开为单个文档

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" } }
$match-匹配所需文档并将其移动到聚合管道中的下一个级别(过滤集合并帮助更快地交付)

$sort-按升序对不需要的文档进行排序(我们需要它,否则在使用Skip和limit时可能会遗漏一些文档)

$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个文档

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" } }

希望有帮助

您可以在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()
MongoClient客户端=新的MongoClient(“localhost”,27017);
MongoDatabase数据库=client.getDatabase(“测试”);
MongoCollection collection=database.getCollection(“collection”);
文档结果=collection.find(Filters.eq(“_id”,4)).projection(Projections.slice(“fingerprint”,1045-256,256)).first()

您可以尝试
db.collection.find({u id:4},{fingerprint:{$slice:[1045-256,256]}})
我正在尝试,但不是活动,谢谢,欢迎您。你犯了什么错误?我能看看你的查询吗?哇,它在运行,我不明白发生了什么:v:v它在MongoShell。如果您了解java+mongo。我可以问你吗??在nodeJS中使用的API库查询在java中也不知道。我正在做一个关于歌曲识别的项目。数据库是完整的。到查询步骤是一个问题。真的谢谢你,这是非常有用的。感谢Clement Amarnath,我尝试了我的数据库,结果非常混乱,使用java非常困难,记录数量非常大