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文件
希望它有助於!
你可以試試'db.collection.find({_id:4},{指紋:{$切片:1045-256,256]}})' – Veeram
我正在嘗試,但沒有活動,謝謝Veeram –
不客氣。你得到了什麼錯誤?我可以看到你的查詢嗎? – Veeram