2017-07-04 80 views
0

在我的集合中,我有兩個包含子文檔的數組。我想刪除這些數組內滿足特定條件的所有子文檔。這是我的文檔的樣子:刪除符合條件的子文檔

{ 
    "_id" : ObjectId("5935a41f12f3fac949a5f925"), 
    "project_id" : 13, 
    "updated_at" : ISODate("2017-07-04T13:10:11.006Z"), 
    "created_at" : ISODate("2017-06-05T18:34:07.150Z"), 
    "uploaded_files" : [ 
     { 
      "display_name" : "CoC_Bot.Main.locale (1).pot", 
      "file" : ObjectId("595b93b298b07d206c0d6153"), 
      "upload_id" : ObjectId("595b93ac98b07d206c0d6152"), 
      "created_at" : ISODate("2017-07-04T13:10:11.000Z") 
     } 
    ], 
    "file_history" : [ 
     { 
      "display_name" : "Log_28-6-2017_14-17-53-562 (1).txt", 
      "file" : ObjectId("595b93ac98b07d206c0d6151"), 
      "upload_id" : ObjectId("595b93ac98b07d206c0d6152"), 
      "created_at" : ISODate("2017-07-04T13:10:04.000Z") 
     }, 
     { 
      "display_name" : "CoC_Bot.Main.locale (1).pot", 
      "file" : ObjectId("595b93b298b07d206c0d6153"), 
      "upload_id" : ObjectId("595b93ac98b07d206c0d6152"), 
      "created_at" : ISODate("2017-07-04T13:10:11.000Z") 
     } 
    ] 
} 

我想從uploaded_filesfile_history匹配upload_id X其刪除所有的子文檔。我怎樣才能做到這一點?

+0

只需通過貓鼬,也可能是蒙戈殼呢? – felipsmartins

+0

貓鼬是我正在尋找的 – kentor

回答

1

可以使用$拉

COLLECTION.update(
    {project_id:12}, /* condition */ 
    {$pull: {file_history:{upload_id: ID}}}, 
    {multi:true, new: true}) 
    .exec(function(err, docs){ 

    }); 
相關問題