我想创建一个多维集合。该集合具有位置。每个位置都有用户,每个用户都有照片。
现在该系列有位置,例如
{ "_id" : ObjectId( "52148266c36b4403e855bef9" ),
"latitude" : 48.958,
"id" : "110600715",
"longitude" : 9.13,
"name" : "Coffee" }
我的最终结果需要是这样的:
{ "_id" : ObjectId( "52148266c36b4403e855bef9" ),
"latitude" : 48.958,
"id" : "110600715",
"longitude" : 9.13,
"name" : "Coffee",
'users' : [
{
'user' : 45,
'username' : 'Me',
'user_fullname': 'Name Lastname',
'photos': [
{
'photo_id' : 10,
'created_time' : 1236456712,
'link' : 'http...',
'image' : 'http...',
'tags' : 'a'
},
{...}
] # end of photos list
},
{...}
] #end of users list
}
如果用户不存在,则创建新用户并添加第一张图片。如果用户存在,则添加具有唯一性的下一张图片photo_id
。
这是我尝试以下的示例。我正在将pymongo与 python 一起使用。我知道我错过了第二个$addToSet
或类似的东西。
db.col.update( {'_id':location_id['_id']},
{ '$addToSet' :
{
'user' : int(i['user']['id']),
'username' : i['user']['username'],
'user_fullname': i['user']['full_name'],
'photos':
{
'photo_id' : i['id'],
'created_time' : int(i['created_time']),
'link' : i['link'],
'image' : i['images']['url'],
'tags' : i['tags']
}
}
}
)