2017-04-21 52 views
0

我有一個這樣的收集數據格式, 我現在傳輸和_id和行。發送_id:如何256元陣列 防爆返回4線1045,從(1045-256)回線1045如何檢索256個元素數組到mongodb?

{ 
    "_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

你可以試試'db.collection.find({_id:4},{指紋:{$切片:1045-256,256]}})' – Veeram

+0

我正在嘗試,但沒有活動,謝謝Veeram –

+0

不客氣。你得到了什麼錯誤?我可以看到你的查詢嗎? – Veeram

回答

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() 
+0

這真的非常有用。謝謝Veeram –

0

MongoDB中聚集管道使用$unwind$match$sort$skip$limit我們可以實現此

MongoDB的殼查詢

db.mycollection.aggregate([{$展開: 「$指紋」},{$匹配:{_id:4}},{$排序:{行:1}} ,{$ skip:1045},{$ limit:256}]);

解釋 - 這怎麼查詢的工作

$放鬆 - 解開指紋陣列成單獨單據

$匹配 - 匹配所需要的文件,它在聚集管道移動到一個新的水平(過濾收集並有助於提供更快的速度)

$ sort - 將展開的文檔按升序排序(我們需要這個,否則我們可能會遺漏使用Skip和限制的幾個文檔)

$跳過 - 需要跳過這是我們已經提供的文件

$限制 - 限制結果的大小

讓我們有一個集合有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" } } 

$放鬆 - unwinded收集和現在我們有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" } } 

$跳過 - 要跳過的文件 $限額 - 要限制最終結果

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文件

希望它有助於!

+0

真的很感謝你,這是非常有用的。謝謝克萊門特Amarnath –

+0

我試着用我的數據庫和結果非常混亂,並使用java很難 –

+0

和數量記錄非常大 –