Mongodb 从子集合中删除属性
我试图使用mongodb从其父集合(列表)的子集合(ListingFeatures)中删除属性(_id)。换句话说,我有一个Listing对象,其中有一个ListingFeature对象列表。ListingFeature有一个我正试图删除的_id属性 以下是我尝试过的命令,我认为它会起作用:Mongodb 从子集合中删除属性,mongodb,Mongodb,我试图使用mongodb从其父集合(列表)的子集合(ListingFeatures)中删除属性(_id)。换句话说,我有一个Listing对象,其中有一个ListingFeature对象列表。ListingFeature有一个我正试图删除的_id属性 以下是我尝试过的命令,我认为它会起作用: db.Listing.update( { "ListingFeatures._id": { $exists:true } }, { $unset: { "ListingFeatures._id": 1 }
db.Listing.update( { "ListingFeatures._id": { $exists:true } }, { $unset: { "ListingFeatures._id": 1 } }, false, true);
在完成之前,它运行了约30分钟(列表有300万条记录),没有抛出任何错误或给出任何类型的消息说它成功或失败,但是Listing.ListingFeatures.\u 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"
}
}
我很确定你不能删除
\u id
(事实上,你甚至不能删除上面的索引)。它是MongoDB保留的字段,用作文档标识符
编辑
好了,我得用这个
让我们删除此集合中someid为“456”的CreateDate
> db.stackoverflow.update({"ListingFeatures.someid":"456"},{$unset:{"ListingFeatures.$.CreateDate":1}});
现在我们验证CreateDate是否从someid为“456”的元素中消失
我很确定你不能删除
\u id
(事实上,你甚至不能删除上面的索引)。它是MongoDB保留的字段,用作文档标识符
编辑
好了,我得用这个
让我们删除此集合中someid为“456”的CreateDate
> db.stackoverflow.update({"ListingFeatures.someid":"456"},{$unset:{"ListingFeatures.$.CreateDate":1}});
现在我们验证CreateDate是否从someid为“456”的元素中消失
对我来说,只有一种工作方式。 即使在孩子里面也要和孩子一起工作
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);
对我来说,只有一种工作方式。 即使在孩子里面也要和孩子一起工作
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);
您是否也尝试了不带查询的更新
{“ListingFeatures.\u id:{$exists:true}
?这行吗?刚刚尝试过,并且_id属性仍然存在:db.Listing.update({},{$unset:{“ListingFeatures._id:1},false,true);您是否也尝试了不带查询的更新{“ListingFeatures.\u id:{$exists:true}
?这行吗?刚刚尝试过,并且_id属性仍然存在:db.Listing.update({},{$unset:{“ListingFeatures._id:1},false,true);我已尝试从子集合中删除另一个属性以进行检查,但该属性也未被删除,因此它与_id无关。您能否粘贴此集合中的文档作为示例?@lobster1234-编辑原始问题以添加示例文档。如我所说,它对_id不起作用。下面是错误:>db.stackoverflow.update({u id:new ObjectId(“4db5aa80a1f7a7b35c69b301”)},{$unset:{u id:1},true,true);Mod on _id不允许正如我所说,我尝试为_id之外的其他属性(如CreateDate)执行此操作,但仍然不起作用。我尝试从子集合中删除另一个属性以进行检查,但该属性也未被删除,所以它与_id无关。你能粘贴这个集合中的文档作为例子吗?@lobster1234-编辑原始问题以添加示例文档。正如我所说,它对_id不起作用。下面是错误:>db.stackoverflow.update({u id:new ObjectId(“4db5aa80a1f7a7b35c69b301”),{$unset:{u id:1},true,true);Mod on _id不允许正如我所说的,我尝试为除_id之外的另一个属性(如CreateDate)执行此操作,但仍然不起作用。