开发者

Remove property from child collection

开发者 https://www.devze.com 2023-02-28 02:44 出处:网络
Using mongodb I\'m trying to remove a property (_id) from a child collection (ListingFeatures) of its parent collection (Listing). To say it another way, I have a Listing object which has a list of Li

Using mongodb I'm trying to remove a property (_id) from a child collection (ListingFeatures) of its parent collection (Listing). To say it another way, I have a Listing object which has a list of ListingFeature objects in it. ListingFeature has an _id property which I'm trying to delete.

Here is the command I tried, which I thought would work:

db.Listing.update( { "ListingFeatures._id": { $exists:true } }, { $unset: { "ListingFeatures._id": 1 } }, false, true);

This ran for ~30 minutes before completing (Listing has 3 million records), it didn't throw any errors or give any sort of message saying it was successful or unsuccessful, however the Listing.ListingFeatures._id property still exists.

What am I missing?

Here is a stripped down sample Listing document. Ultimately I want to remove _id, CreateDate, and UpdateDate from the ListingFeatures collections.

{
  "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开发者_Go百科",
      "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"
  }
}


Only one way with work for me. Work even with child inside child.

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);


I am pretty sure you cannot remove _id (in fact, you cannot even remove the index on it). It is reserved field by MongoDB and acts as a document identifier.

EDIT

There you go - had to use the $ 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"
}

Lets remove the CreateDate where someid is "456" within this collection.

> db.stackoverflow.update({"ListingFeatures.someid":"456"},{$unset:{"ListingFeatures.$.CreateDate":1}});                                                    

Now we verify that the CreateDate is gone from the element where someid is "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")
}
0

精彩评论

暂无评论...
验证码 换一张
取 消