多次元のコレクションを作りたい。コレクションには場所があります。各場所にはユーザーがいて、各ユーザーには写真があります。
現在、コレクションには場所があります。
{ "_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
。
これは私が試したサンプルです。私はpythonでpymongoを使用しています。私は1秒$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']
}
}
}
)