使用mongodb我试图从其父集合(清单)的子集合(ListingFeatures)中删除属性(_id)。换句话说,我有一个Listing对象,其中包含一个ListingFeature对象列表。 ListingFeature有一个_id属性,我试图删除它。
这是我试过的命令,我认为这可以起作用:
db.Listing.update( { "ListingFeatures._id": { $exists:true } }, { $unset: { "ListingFeatures._id": 1 } }, false, true);
这在完成之前运行了大约30分钟(列表有300万条记录),它没有抛出任何错误或提供任何类型的消息说它成功或不成功,但是Listing.ListingFeatures._id属性仍然存在。
我错过了什么?
这是一个精简的样本列表文档。最后,我想从ListingFeatures集合中删除_id,CreateDate和UpdateDate。
{
"Address": "1080 DUNLAP ORPHANAGE RD",
"City": "Unincorporated",
"ListingFeatures": [
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Status: ACTIVE"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "County: Tipton"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Approximately 10 acre(s)"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "2 total full bath(s)"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "2 total half bath(s)"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "9 total rooms"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "2 stories"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Type: General Residential"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Master Bedroom is Carpet, Full Bath, Level 1, Walk-In Closet"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Basement is Partial, Unfinished"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Pool features: Above Ground"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "2 covered parking space(s)"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Yes car garage(s)"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Attached parking"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Cooling features: 220 Wiring, Dual System,Cooling"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Inclusions: Cooktop, Dishwasher, Gas Cooking, Microwave, Refrigerator, Satellite Dish, Self Cleaning Oven"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Lot features: Chain Fenced, Landscaped, Level, Some Trees, Wooded"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Lot size is between 10 and 20 acres"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Utilities present: Public Water, Septic Tank"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Swimming pool(s)"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Basement"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Den"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Laundry room"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Hardwood floors"
},
{
"_id": null,
"CreateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate": "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name": "Parking features: Driveway/Pad, Garage Door Opener(s), Storage Room(s), Workshop(s)"
}
],
"ZipCode": "38011",
"_id": {
"$oid": "4d99055b1b9ba917bcef9443"
}
}
答案 0 :(得分:0)
我很确定你无法删除_id
(事实上,你甚至无法删除它上面的索引)。它是MongoDB的保留字段,用作文档标识符。
修改强>
你去了 - 必须使用$ Position Operator
。
> db.stackoverflow.findOne()
{
"_id" : ObjectId("4db6362c9e631c2a52a7c645"),
"Address" : "1080 DUNLAP ORPHANAGE RD",
"City" : "Unincorporated",
"ListingFeatures" : [
{
"someid" : "123",
"CreateDate" : "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate" : "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name" : "Approximately 10 acre(s)"
},
{
"someid" : "456",
"CreateDate" : "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate" : "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name" : "2 total full bath(s)"
}
],
"ZipCode" : "38011"
}
让我们删除其中someid为“456”的CreateDate。
> db.stackoverflow.update({"ListingFeatures.someid":"456"},{$unset:{"ListingFeatures.$.CreateDate":1}});
现在我们验证CreateDate是否从someid为“456”的元素中消失。
> db.stackoverflow.findOne()
{
"Address" : "1080 DUNLAP ORPHANAGE RD",
"City" : "Unincorporated",
"ListingFeatures" : [
{
"someid" : "123",
"CreateDate" : "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"UpdateDate" : "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"Name" : "Approximately 10 acre(s)"
},
{
"Name" : "2 total full bath(s)",
"UpdateDate" : "Sun, 03 Apr 2011 19:40:04 GMT -04:00",
"someid" : "456"
}
],
"ZipCode" : "38011",
"_id" : ObjectId("4db6362c9e631c2a52a7c645")
}
答案 1 :(得分:0)
只有一种方式可以帮助我。 甚至与孩子内部的孩子一起工作。
var updated_comment = 0;
db.Document.find().forEach(function(doc){
function remove_errors(comments){
comments.forEach(function(comment){
delete comment.errors
updated_comment = updated_comment + 1;
remove_errors(comment.comments);
});
}
remove_errors(doc.comments);
db.Document.save(doc);
});
print("done updated_comment = " + updated_comment);