简体   繁体   English

如何在 mongoDB 中提取嵌入文档中的特定元素

[英]How to pull specific element in embedded document in mongoDB

I have a MongoDB embedded document where it has many documents in arrays.我有一个 MongoDB 嵌入式文档,它在数组中有许多文档。

EX:前任:

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

I have many documents with the combination of UnitID and TypeID (inside Types object).我有很多文档组合了 UnitID 和 TypeID(在 Types 对象中)。 I have to delete that Type element by match UnitId and TypeID.我必须通过匹配 UnitId 和 TypeID 来删除该 Type 元素。 I have used $pull but didn't achieve.我用过 $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}

    );

How to achieve the delete the element inside that types array where that Type order in Types array is changing.如何实现删除该类型数组中的元素,其中 Types 数组中的 Type 顺序正在发生变化。

You can use either of below queries to query the nested arrays.您可以使用以下任一查询来查询嵌套数组。

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

OR ( More applicable for multi criteria scenario ) 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}
 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mongodb arrays - 从嵌入文档中提取 - Mongodb arrays - $pull from embedded document 如何在 MongoDB 中的数组中获取嵌入文档(使用 Mongoose) - How to get embedded document in an array in MongoDB (with Mongoose) 如何在MongoDb中的嵌入式文档中删除数组上的“属性”? - How to remove “attribute” on array in embedded document in MongoDb? 如何通过MongoDB PHP中的嵌入式项目查找文档 - How to find a document by embedded item in MongoDB PHP 如何在 MongoDB 中的数组中查询单个嵌入文档? - How to query a single embedded document in an array in MongoDB? 通过指示值从MongoDB文档中提取1级深度数组元素 - Pull 1 level deep array element from MongoDB document by indicating value MongoDB:更新子文档中的特定数组元素 - MongoDB: Updating A specific array element in a sub document MongoDB 罗盘:如何过滤 MongoDB 文档中的嵌入式数组 object - MongoDB Compass: How to filter embedded array object in the MongoDB document 如何查询包含数组MongoDB数组中某些元素的特定值的单个文档 - How to query single document which contains specific value of some element in array of arrays MongoDB 如何从 mongodb 的文档数组中提取嵌套文档? - How to pull nested document from the array of documents in mongodb?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM