【发布时间】:2014-10-24 02:02:51
【问题描述】:
我正在开发 mongo,我想做以下事情:
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
}
]
}]
我尝试做以下事情,但显然它没有像我想象的那样工作。
db.zones.update({'lat': 90}, {$push: {lons: {'uid' : 0, 'lon': -18}}}, { upsert: true })
db.zones.ensureIndex({'lat': -1, 'lons.lon':1}, {unique: true})
我检查了这篇帖子 Can mongo upsert array data? 和其他一些帖子,但不知何故,它们都没有工作。我不知道这是我的问题还是 mongo 的问题。谢谢!
【问题讨论】:
标签: mongodb insert mongoose upsert subdocument