2011-04-23 84 views
2

使用mongodb我試圖從其父集合(Listing)的子集合(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

你是否還試過沒有查詢'{「ListingFeatures._id」:{$ exists:true}}'的查詢?那樣有用嗎? – proximus 2011-04-24 08:50:15

+0

剛試過,_id屬性仍然存在:db.Listing.update({},{$ unset:{「ListingFeatures._id」:1}},false,true); – Justin 2011-04-24 14:29:22

回答

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

讓我們刪除CreateDate,其中someid在此集合中是「456」。

> 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") 
} 
+0

我試圖從子集合中刪除另一個屬性來檢查,並且該屬性也沒有被刪除,所以它與_id無關。 – Justin 2011-04-25 03:05:15

+0

作爲一個例子,你能粘貼這個集合的文檔嗎? – lobster1234 2011-04-25 03:25:46

+0

@ lobster1234 - 編輯原始問題添加示例文檔。 – Justin 2011-04-25 13:08:57