I\'m a MongoDB newbie and wanted to ask how to write an update command involving upsert and list.
Basically I want to accomplish something like this:
You can't mix the positional operator ("$") and an upsert; the "$" will be treated as a field name during the insert. You can't do this for new documents, only existing one.
I suggested a structure more like this:
{"_id" : ObjectId("4c28f62cbf8544c60506f11d"),
"some_other_data":"goes here",
"trips": {
"2010-05-10":
[{"lat":21.321231, "lng": 16.8783234, "updated_at": "Mon May 10 2010 15:24:35"},
{"lat":21.321231, "lng": 16.8783234, "updated_at": "Mon May 10 2010 15:24:24"}],
"2010-05-08":
[{"lat":21.324239, "lng": 16.8735234, "updated_at": "Mon May 8 2010 11:18:05"},
{"lat":21.311234, "lng": 16.8743271, "updated_at": "Mon May 8 2010 11:17:55"},
{"lat":21.321238, "lng": 16.8782219, "updated_at": "Mon May 8 2010 11:17:45"}]
}
}
Then you can issue an update like this:
db.mycollection.update({application_id: "MyTestApp", "trips.2010-05-10":{$exists:true}},
{$push: {"trips.2010-05-10": {lat:11, lng:11} }},
true);
results in this being inserted.
> db.mycollection.find()
{ "_id" : ObjectId("4c2931d17b210000000045f0"),
"application_id" : "MyTestApp",
"trips" : { "2010-05-10" : [ { "lat" : 11, "lng" : 11 } ] } }
and running it again give you this:
> db.mycollection.find()
{ "_id" : ObjectId("4c2932db7b210000000045f2"),
"application_id" : "MyTestApp",
"trips" : { "2010-05-10" :
[ { "lat" : 11, "lng" : 11 },
{ "lat" : 11, "lng" : 11 } ] } }