在我的收藏中,我有两个带有子文档的数组。我想删除符合特定条件的这些数组中的所有子文档。这就是我的文档的样子:
{
"_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_files
和file_history
中与upload_id x匹配的所有子文档。我怎样才能做到这一点?
答案 0 :(得分:3)
您可以使用$ pull
COLLECTION.update(
{project_id:12}, /* condition */
{$pull: {file_history:{upload_id: ID}}},
{multi:true, new: true})
.exec(function(err, docs){
});