2017-05-24 80 views
1

「如何刪除從結構嵌套數組‘帳戶’的單個元素(顯示的圖像也)蒙戈分貝。我能做到使用$拉法呢?任何一個知道請分享你的想法「。如何刪除嵌套數組元素蒙戈Db的

"associatedAccounts" : [ 
     { 
      "organizationId" : "5808ba773fe315441b9e0a9e", 
      "_id" : ObjectId("5808bc0c3fe315441b9e0b1a"), 
      "accounts" : [ 
       "5808baf33fe315441b9e0aa7", 
       "5808baf33fe315441b9e0aa8", 
       "5808baf33fe315441b9e0aa1" 
      ] 
     }, 
     { 
      "organizationId" : "5808ba773fe315441b9e0a9f", 
      "_id" : ObjectId("5808bc0c3fe315441b9e0b1b"), 
      "accounts" : [ 
       "5808baf33fe315441b9e0aa3", 
       "5808baf33fe315441b9e0aa2", 
       "5808baf33fe315441b9e0aa5", 
       "5808baf33fe315441b9e0aa6" 
      ] 
     } 
    ] 

enter image description here

+0

https://stackoverflow.com/editing-help –

回答

0

您可以刪除的_id的基礎上,因爲ID是唯一在MongoDB中識別

db.stores.update(
{_id:id }, //set accounts Id which you want to update 
{$pull:{ 
     "associatedAccounts":{ 
        "_id"accountsId // the unique _id from the objects in arrray 
        } 
     } 
}) 

accountsId將從associatedAccounts唯一的ID,即每個對象的_id這是目前在這個數組中。

$pull將刪除perticular對象,其ID匹配查詢。

+0

這是不正確的方法,它不會爲我所提到的情況下工作 – vijesh