-1

Code created in Mongoose to update a subdocument was not working. So I tried to update the subdocument within the Mongo Shell.
This is the document (location) and subdocument (review):

{
"_id" : ObjectId("56d8c73314fbc7e702cfb8c4"),
"name" : "Costly",
"address" : "150, Super Street",
"coords" : [
    -0.9630884,
    51.451041
],
"reviews" : [
    {
        "author" : "kyle riggen1",
        "_id" : ObjectId("56d8de74cc7f953efd8455d9"),
        "rating" : 4,
        "timestamp" : ISODate("2015-06-01T06:00:00Z"),
        "reviewText" : "will the ID work?"
    }
],
"rating" : 0,
"__v" : 2

}

Here are some of my attempts at updating the subdocument:
This question gave this format:

update({ 
       _id: "56d8c73314fbc7e702cfb8c4", 
       "reviews._id": ObjectId("56d8de74cc7f953efd8455d9")
   },{
       $set: {"reviews.$.rating": 1}
   }, false, true
);

This returned an error of "update is not defined" as shown:

2016-03-03T22:52:44.445-0700 E QUERY    [thread1] ReferenceError: update is not defined :
@(shell):1:1

Which I think is because the command did not start with db.locations.update()

MongoDB documentation used this format:

db.locations.update(
   {
     _id: "56d8c73314fbc7e702cfb8c4",
     review: { $elemMatch: { author: "kyle riggen1" } }
   },
   { $set: { "location.$.rating" : 1 } }
)

This returns a valid update but the update didn't actually happen as shown:

WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

This question used this format:

db.locations.update({
    _id: "56d8c73314fbc7e702cfb8c4",
    'review.author': 'kyle riggen1'
  },
  { $set: { 'review.$.rating': 1 }}
)

This returns the same as the MongoDB documentation as shown here:

WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

So these queries I guess are working but my data is not getting updated. Would my data be indexed wrong perhaps? The actual location is able to be updated even through a Mongoose API.

Community
  • 1
  • 1
KyleRiggen
  • 121
  • 7
  • Woul'd you like to take another crack at choosing a title that isn't going to offend anyone or otherwise embarass you when people give the simple explanation? And of course that explaination is `ObjectId("56d8c73314fbc7e702cfb8c4")`. Oops, you forgot to cast your string. – Blakes Seven Mar 04 '16 at 06:04
  • @BlakesSeven fixed title to a more appropriate question. The ObjectId() only appears in my mongo shell. The API produces an ID without it. I have edited my question to reflect this. – KyleRiggen Mar 04 '16 at 06:12
  • What API? All of your examples ( and every error and response ) are being issed from the MongoDB shell. The values you are passing to `_id` in the query need to be an `ObjectId`, otherwise they will not match. That's exactly what is happening here. Only something like `mongoose` will autocast a string as it's schema type for you, but you are not executing any of your samples in mongoose. – Blakes Seven Mar 04 '16 at 06:14
  • @BlakesSeven I see ok giving that a try – KyleRiggen Mar 04 '16 at 06:17
  • @BlakesSeven And that worked! Would you mind submitting an answer so I can give you credit to my silly question? – KyleRiggen Mar 04 '16 at 06:24
  • I actually marked it for a close already, since a fairly clear omission of casting to an `ObjectId` is a pretty basic mistake, and already credited to answers and questions submitted many years before. Next time, pay attention. – Blakes Seven Mar 04 '16 at 08:53

2 Answers2

1

You can do it By $push Or $addToSet.

db.col.update(
        { name: 'reviews', 'list.id': 2 }, 
        {$push: {'list.$.items': {id: 5, name: 'item5'}}}
    )

See the reference from mongodb Manual

https://docs.mongodb.org/manual/reference/operator/update/push/

Istiak Morsalin
  • 10,621
  • 9
  • 33
  • 65
-1

Please know db.collection.update( criteria, objNew, upsert, multi )

  1. criteria: match condition
  2. objNew: update content
  3. upsert: true or false
    • true : if not existed, insert it
    • false : if not existed, don't insert
  4. multi: true or false
    • true : update all matched documents
    • false : only update the first matched document
zero298
  • 25,467
  • 10
  • 75
  • 100
Superman
  • 285
  • 5
  • 17
  • I'm sorry ,this is my first answer. I am not familiar with this forum。 I can familiar with this as soons as possible – Superman Nov 23 '16 at 02:18