I was working on mongo and I'd like to do the following things:
when a set of (lat, lon, uid) comes in:
1. the collection has lat as unique index, also for each lat the lon index is unique
2. if (lat, lon) pair exists in this collection, update uid in the sub-document
3. if (lat) exists in this document , insert (lon, uid) in the lons sub-document
4. if (lat) document doesn't exist, create lat document and do 2
[{
"lat" : 1, (doc is unique by lat)
"lons" : [
{
"lon" : 2, (the subdocument is unique by lon)
"uid" : 3
},
{
"lon" : 3,
"uid" : 3
}
]
},
{
"lat" : 2,
"lons" : [
{
"lon" : 2,
"uid" : 4
}
]
}]
I tried to do the following things but apparently it's not working as what I imagined.
db.zones.update({'lat': 90}, {$push: {lons: {'uid' : 0, 'lon': -18}}}, { upsert: true })
db.zones.ensureIndex({'lat': -1, 'lons.lon':1}, {unique: true})
I checked this post Can mongo upsert array data? and some others but somehow none of them is working. I don't know if it's my problem or mongo problem. Thanks!