亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

僅檢索MongoDB集合中對象數組中的查詢元素

僅檢索MongoDB集合中對象數組中的查詢元素

幕布斯6054654 2019-05-21 13:56:35
僅檢索MongoDB集合中對象數組中的查詢元素假設我的收藏中有以下文件:{     "_id":ObjectId("562e7c594c12942f08fe4192"),   "shapes":[        {           "shape":"square",         "color":"blue"      },      {           "shape":"circle",         "color":"red"      }   ]},{     "_id":ObjectId("562e7c594c12942f08fe4193"),   "shapes":[        {           "shape":"square",         "color":"black"      },      {           "shape":"circle",         "color":"green"      }   ]}查詢:db.test.find({"shapes.color": "red"}, {"shapes.color": 1})要么db.test.find({shapes: {"$elemMatch": {color: "red"}}}, {"shapes.color": 1})返回匹配的文檔(文檔1),但始終包含所有數組項shapes:{ "shapes":   [    {"shape": "square", "color": "blue"},    {"shape": "circle", "color": "red"}  ] }但是,我想僅使用包含以下內容的數組來獲取文檔(文檔1)color=red:{ "shapes":   [    {"shape": "circle", "color": "red"}  ] }我怎樣才能做到這一點?
查看完整描述

4 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

MongoDB 2.2的新$elemMatch投影操作符提供了另一種方法來更改返回的文檔以僅包含第一個匹配的shapes元素:

db.test.find(
    {"shapes.color": "red"}, 
    {_id: 0, shapes: {$elemMatch: {color: "red"}}});

返回:

{"shapes" : [{"shape": "circle", "color": "red"}]}

在2.2中,您也可以使用$ projection operator,其中$投影對象字段名稱表示字段中查詢的第一個匹配數組元素的索引。以下返回與上面相同的結果:

db.test.find({"shapes.color": "red"}, {_id: 0, 'shapes.$': 1});

MongoDB 3.2更新

從3.2版本開始,您可以使用新的$filter聚合運算符在投影期間過濾數組,這樣可以包含所有匹配,而不僅僅是第一個匹配。

db.test.aggregate([
    // Get just the docs that contain a shapes element where color is 'red'
    {$match: {'shapes.color': 'red'}},
    {$project: {
        shapes: {$filter: {
            input: '$shapes',
            as: 'shape',
            cond: {$eq: ['$$shape.color', 'red']}
        }},
        _id: 0
    }}])

結果:

[ 
    {
        "shapes" : [ 
            {
                "shape" : "circle",
                "color" : "red"
            }
        ]
    }]


查看完整回答
反對 回復 2019-05-21
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

MongoDB 2.2+中的新聚合框架提供了Map / Reduce的替代方案。該$unwind操作可用于分離的shapes陣列到的文件流可以匹配:

db.test.aggregate(
  // Start with a $match pipeline which can take advantage of an index and limit documents processed
  { $match : {
     "shapes.color": "red"
  }},
  { $unwind : "$shapes" },
  { $match : {
     "shapes.color": "red"
  }})

結果是:

{
    "result" : [
        {
            "_id" : ObjectId("504425059b7c9fa7ec92beec"),
            "shapes" : {
                "shape" : "circle",
                "color" : "red"
            }
        }
    ],
    "ok" : 1}


查看完整回答
反對 回復 2019-05-21
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

另一種有趣的方法是使用$ redact,這是MongoDB 2.6的新聚合功能之一。如果您使用的是2.6,則不需要$ unwind,如果您有大型數組,可能會導致性能問題。

db.test.aggregate([
    { $match: { 
         shapes: { $elemMatch: {color: "red"} } 
    }},
    { $redact : {
         $cond: {
             if: { $or : [{ $eq: ["$color","red"] }, { $not : "$color" }]},
             then: "$$DESCEND",
             else: "$$PRUNE"
         }
    }}]);

$redact “根據文件本身存儲的信息限制文件的內容”。所以它只會在文檔內部運行。它基本上掃描你的文檔頂部到底部,并檢查它是否與你的if條件匹配$cond,如果有匹配,它將保留content($$DESCEND)或remove($$PRUNE)。

在上面的示例中,首先$match返回整個shapes數組,$ redact將其刪除到預期結果。

請注意,這{$not:"$color"}是必要的,因為它也將掃描頂層文檔,如果在頂層$redact找不到color字段,則返回false可能會刪除我們不想要的整個文檔。


查看完整回答
反對 回復 2019-05-21
  • 4 回答
  • 0 關注
  • 3528 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號