-1

I have a collection like this

{ 
    "_id" : ObjectId("5446757568567546456"), 
    "name" : "Duck-trailer", 
    "data" : [
        {
            "date" : "1-1-2015", 
            "oldPrice" : 10,
            "price" : 15
        }, 
        {
            "date" : "2-1-2015", 
            "oldPrice" : 10
            "price" : 12
        }
      ]
}

So I try to select by id and date , then update the price by selecting the date which is nested within the data object.

My query did not work, got error of MongoError: cannot use the part..

myModel.update({_id: id, 'data.$.date':'2-1-2015'}, {'$set': {'data.$.price': 100}}, 
    function(err,result) {
})
Cassie
  • 49
  • 1
  • 7

1 Answers1

1

while matching you dont need the positional operator.

db.collection.update({"data.date":"2-1-2015"},{$set:{"data.$.price":100}})

this will set the price to 100 where there is a match for the date you entered.

satish chennupati
  • 2,602
  • 1
  • 18
  • 27