【问题标题】:How to pull specific element in embedded document in mongoDB如何在 mongoDB 的嵌入式文档中提取特定元素
【发布时间】:2018-08-03 15:18:25
【问题描述】:

我有一个 MongoDB 嵌入式文档,其中包含许多数组文档。

前:

{
    "_id" : ObjectId("594b7b7d4064e264420e38d0"),
    "UnitId" : "594b75d44064e264420e3869",
    "UnitName" : "PARKING",
    "UnitIcon" : "./upload/file-1498117588607.jpg", 
    "Stream" : {
        "_id" : ObjectId("594b7b7d4064e264420e38d1"),
        "Types" : [ 
            {
                "TypeId" : "594b9a884064e264420e3b56",
                "TypeName" : "TR/RC",
                "Travel" : false
            }, 
            {
                "TypeId" : "594b9a844064e264420e3b55",
                "MaterialStreamName" : "RW",              
                "Travel_Required" : false
            }, 
            {
                "TypeId" : "594b9a9d4064e264420e3b58",
                "TypeName" : "ST",               
                "Travel_Required" : true
            }, 
            {
                "TypeId" : "594b9a764064e264420e3b53",
                "TypeName" : "FD",               
                "Travel_Required" : true
            }
        ]
    }   
},{
    "_id" : ObjectId("594b7b7d4064e264420e38d1"),
    "UnitId" : "594b75d44064e264420e3870",
    "UnitName" : "CAFE",
    "UnitIcon" : "./upload/file-1498117588608.jpg", 
    "Stream" : {
        "_id" : ObjectId("594b7b7d4064e264420e38d2"),
        "Types" : [ 
            {
                "TypeId" : "594b9a884064e264420e3b56",
                "TypeName" : "TR/RC",
                "Travel" : false
            }, 
            {
                "TypeId" : "594b9a844064e264420e3b55",
                "MaterialStreamName" : "RW",              
                "Travel_Required" : false
            }, 
            {
                "TypeId" : "594b9a9d4064e264420e3b58",
                "TypeName" : "ST",               
                "Travel_Required" : true
            }, 
            {
                "TypeId" : "594b9a764064e264420e3b53",
                "TypeName" : "FD",               
                "Travel_Required" : true
            }
        ]
    }   
},{
    "_id" : ObjectId("594b7b7d4064e264420e38d2"),
    "UnitId" : "594b75d44064e264420e3870",
    "UnitName" : "CAFE",
    "UnitIcon" : "./upload/file-1498117588608.jpg", 
    "Stream" : {
        "_id" : ObjectId("594b7b7d4064e264420e38d3"),
        "Types" : [                 
            {
                "TypeId" : "594b9a9d4064e264420e3b58",
                "TypeName" : "ST",               
                "Travel_Required" : true
            },     
            {
                    "TypeId" : "594b9a884064e264420e3b56",
                    "TypeName" : "TR/RC",
                    "Travel" : false
                }, 
                {
                    "TypeId" : "594b9a844064e264420e3b55",
                    "MaterialStreamName" : "RW",              
                    "Travel_Required" : false
                }, 
                {
                    "TypeId" : "594b9a764064e264420e3b53",
                    "TypeName" : "FD",               
                    "Travel_Required" : true
                }
            ]
        }   
    }

我有许多包含 UnitID 和 TypeID 组合的文档(在 Types 对象内)。我必须通过匹配 UnitId 和 TypeID 来删除该 Type 元素。我使用了 $pull 但没有实现。

    db.getCollection('units_details').update(
    { "UnitId" : "594b75d44064e264420e3870","Stream[0].Types.$.TypeID" : "594b9a884064e264420e3b56"    }, 
    { $pull: { "Stream[0].Types"            
                 "TypeId" : "594b9a884064e264420e3b56",
                        "TypeName" : "TR/RC",                            
                        "Travel" : false
          } }} ,{multi:true}

    );

如何实现删除types数组中types数组中的type顺序发生变化的元素。

【问题讨论】:

  • 您的文件是否正确?我没看到Material_Stream_Flow_Details
  • @Veeram 已编辑,请立即查看
  • db.getCollection('units_details').update( { "UnitId" : "594b75d44064e264420e3870"}, { $pull: { "Stream.Types" :{ "TypeId" : "594b9a884064e264420e3b56", "TypeName" : "TR/RC", "Travel" : false } } },{multi:true} ); 之类的东西应该可以工作
  • @Veeram 流还有一个数组有一些元素。类型在 [0] 位置
  • 请更新您的文件。它将流显示为嵌入式文档

标签: arrays database mongodb mongoose mongodb-query


【解决方案1】:

您可以使用以下任一查询来查询嵌套数组。

db.getCollection('units_details').update(
  {"UnitId":"594b75d44064e264420e3870","Stream.Types.TypeId":"594b9a884064e264420e3b56"},
  {"$pull":{
    "Stream.$.Types":{
      "TypeId":"594b9a884064e264420e3b56",
      "TypeName":"TR/RC",
      "Travel":false}
  }},
  {"multi":true}
);

OR(更适用于多条件场景)

db.getCollection('units_details').update( {
    "UnitId":"594b75d44064e264420e3870",
    "Stream":{
      "$elemMatch":{
        "Types":{
          "$elemMatch":{"TypeId":"594b9a884064e264420e3b56"}
        }
      }
    }
  },
  {"$pull":{
    "Stream.$.Types":{
      "TypeId":"594b9a884064e264420e3b56",
      "TypeName":"TR/RC",
      "Travel":false}
  }},
  {"multi":true}
 )

【讨论】:

  • 感谢您的快速帮助。第二个查询正在工作,但第一个查询不工作,我怀疑这是由于条件Stream.Types.TypeId 中的类型的位置。
  • 你能帮忙解决这个问题吗stackoverflow.com/q/51474838/5756149
猜你喜欢
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 2020-11-22
  • 2020-03-08
  • 1970-01-01
相关资源
最近更新 更多