I’ve recently updated to Mongodb 2.2.0 and found that the following query now no longer works.
The database is empty and I run the following:
db.Sessions.update({_id:"test",sessions:{$not:{$elemMatch:{type:"Web"}}}},{$push:{sessions:{type:"Web",dateAdded:new Date}}},true)
returns the error:
Cannot apply $push/$pushAll modifier to non-array
Prior to the update the following document would have been created:
{ "_id" : "test", "sessions" : [ { "type" : "Web", "dateAdded" : ISODate("2012-09-12T15:11:11.942Z") } ] }
Any ideas?
Edit:
I forgot to mention, it’s the addition of the $not that breaks in this version as the following works fine, so it’s not a issue that the array/field doesn’t exist.
db.Sessions.update({_id:"test"},{$push:{sessions:{type:"Web",dateAdded:new Date}}},true)
The behavior of
$elemMatchhas changed slightly in 2.2, which is why this no longer works in combination with$not, which is only a meta-operator for negating other operators and can not actually be used to search fields.Instead, you can use
$nin(not in) to inspect the values inside your sessions array.For example,