> db.customers.insert({"FirstName":"David","FamilyName":"Marceau","PurchaseHistory":"nil"})
> db.customers.find()
{ "_id" : ObjectId("51f98272cb285502da80d468"), "FirstName" : "David", "FamilyName" : "Marceau", "PurchaseHistory" : "nil" }
> db.customers.update({},{$set: {PurchaseHistory: "blah"} },{multi:true})
> db.customers.find()
{ "FamilyName" : "Marceau", "FirstName" : "David", "PurchaseHistory" : "blah", "_id" : ObjectId("51f98272cb285502da80d468") }
> db.customers.update({},{$set: {PurchaseHistory: { cart1:1, cart2:2, cart3:3} } },{multi:true})
> db.customers.find()
{ "FamilyName" : "Marceau", "FirstName" : "David", "PurchaseHistory" : { "cart1" : 1, "cart2" : 2, "cart3" : 3 }, "_id" : ObjectId("51f98272cb285502da80d468") }
> db.customers.update({},{$set: {PurchaseHistory: { cart1: { shopitem1: 1, shopitem2:2, shopitem3:3}, cart2:{shopitem1:4, shopitem2:5, shopitem3:6}, cart3:{shopitem7:7,shopitem8:8,shopitem9:9}} } },{multi:true})
> db.customers.find()
{ "FamilyName" : "Marceau", "FirstName" : "David", "PurchaseHistory" : { "cart1" : { "shopitem1" : 1, "shopitem2" : 2, "shopitem3" : 3 }, "cart2" : { "shopitem1" : 4, "shopitem2" : 5, "shopitem3" : 6 }, "cart3" : { "shopitem7" : 7, "shopitem8" : 8, "shopitem9" : 9 } }, "_id" : ObjectId("51f98272cb285502da80d468") }
Here is where I have difficulty. How do I replace cart3?
> db.customers.update({},{$set: {cart3:{shopitem7:10,shopitem8:11,shopitem9:12}} },{multi:false})
> db.customers.find()
{ "FamilyName" : "Marceau", "FirstName" : "David", "PurchaseHistory" : { "cart1" : { "shopitem1" : 1, "shopitem2" : 2, "shopitem3" : 3 }, "cart2" : { "shopitem1" : 4, "shopitem2" : 5, "shopitem3" : 6 }, "cart3" : { "shopitem7" : 7, "shopitem8" : 8, "shopitem9" : 9 } }, "_id" : ObjectId("51f98272cb285502da80d468"), "cart3" : { "shopitem7" : 10, "shopitem8" : 11, "shopitem9" : 12 } }
That results with two cart3's. Is that an instance of $set: PurchaseHistory.cart3 {"shopitem7" : 10, "shopitem8" : 11, "shopitem9" : 12}?
> db.customers.find()
{ "FamilyName" : "Marceau", "FirstName" : "David", "PurchaseHistory" : { "cart1" : { "shopitem1" : 1, "shopitem2" : 2, "shopitem3" : 3 }, "cart2" : { "shopitem1" : 4, "shopitem2" : 5, "shopitem3" : 6 }, "cart3" : { "shopitem7" : 7, "shopitem8" : 8, "shopitem9" : 9 } }, "_id" : ObjectId("51f98272cb285502da80d468"), "cart3" : { "shopitem7" : 10, "shopitem8" : 11, "shopitem9" : 12 } }
> db.customers.find(ObjectId("51f98272cb285502da80d468"))
{ "FamilyName" : "Marceau", "FirstName" : "David", "PurchaseHistory" : { "cart1" : { "shopitem1" : 1, "shopitem2" : 2, "shopitem3" : 3 }, "cart2" : { "shopitem1" : 4, "shopitem2" : 5, "shopitem3" : 6 }, "cart3" : { "shopitem7" : 7, "shopitem8" : 8, "shopitem9" : 9 } }, "_id" : ObjectId("51f98272cb285502da80d468"), "cart3" : { "shopitem7" : 10, "shopitem8" : 11, "shopitem9" : 12 } }
> db.customers.update({_id:ObjectId("51f98272cb285502da80d468")},{$set: {cart3:{shopitem7:13,shopitem8:14,shopitem9:15}} },{multi:false})
> db.customers.find(ObjectId("51f98272cb285502da80d468"))
{ "FamilyName" : "Marceau", "FirstName" : "David", "PurchaseHistory" : { "cart1" : { "shopitem1" : 1, "shopitem2" : 2, "shopitem3" : 3 }, "cart2" : { "shopitem1" : 4, "shopitem2" : 5, "shopitem3" : 6 }, "cart3" : { "shopitem7" : 7, "shopitem8" : 8, "shopitem9" : 9 } }, "_id" : ObjectId("51f98272cb285502da80d468"), "cart3" : { "shopitem7" : 13, "shopitem8" : 14, "shopitem9" : 15 } }
> db.customers.update({_id:ObjectId("51f98272cb285502da80d468")},{$unset: {cart3:{shopitem7:13,shopitem8:14,shopitem9:15}} },{multi:false})
> db.customers.find(ObjectId("51f98272cb285502da80d468"))
{ "FamilyName" : "Marceau", "FirstName" : "David", "PurchaseHistory" : { "cart1" : { "shopitem1" : 1, "shopitem2" : 2, "shopitem3" : 3 }, "cart2" : { "shopitem1" : 4, "shopitem2" : 5, "shopitem3" : 6 }, "cart3" : { "shopitem7" : 7, "shopitem8" : 8, "shopitem9" : 9 } }, "_id" : ObjectId("51f98272cb285502da80d468") }
> db.customers.update({_id:ObjectId("51f98272cb285502da80d468")},{$unset: cart3 },{multi:false})
Wed Jul 31 17:58:09 ReferenceError: cart3 is not defined (shell):1
What's wrong with doing this? Why do I need to restate the values that were stored there when I want to remove them?
> db.customers.update({_id:ObjectId("51f98272cb285502da80d468")},{$unset: {PurchaseHistory} },{multi:false})
Wed Jul 31 18:00:02 SyntaxError: missing : after property id (shell):1
What's wrong with doing this? Why do I need to restate the values that were stored there when I want to remove them?
The only thing I succeeded in doing was to rewrite the entire contents for the specified objectid.
> db.customers.update({_id:ObjectId("51f98272cb285502da80d468")},{"FamilyName" : "Marceau", "FirstName" : "David", "PurchaseHistory" : { "cart1" : { "shopitem1" : 1, "shopitem2" : 2, "shopitem3" : 3 } } },{multi:false})
> db.customers.find(ObjectId("51f98272cb285502da80d468"))
{ "_id" : ObjectId("51f98272cb285502da80d468"), "FamilyName" : "Marceau", "FirstName" : "David", "PurchaseHistory" : { "cart1" : { "shopitem1" : 1, "shopitem2" : 2, "shopitem3" : 3 } } }
>
Thanks again for taking the time.