[英]MongoDB python nested objects
There is such a structure 有这样的结构
PLAYER_DEFAULT = {
"_id": Inc(PlayersDB),
"mail": "test@gmail.com",
"password": "123456",
"token": uuid4().hex,
"username": "applet",
"user_info":
{
"show_name" : "Hero",
"rate_us": 20000,
"rating": 300,
"gcg": "",
"ration":0,
"items":
[
{"id":1, "item_type":"socket", "name":"aEveA", "data":{"level":1, "stat":"AVA"}},
{"id":2, "item_type":"socket", "name":"aEveA", "data":{"level":4, "stat":"AVA"}},
{"id":3, "item_type":"socket", "name":"Hestos", "data":{"level":9, "stat":"Hest"}},
{"id":4, "item_type":"user", "name":"AAACZX", "data":{"expr":1000}},
{"id":5, "item_type":"user", "name":"AAAAZZZCX", "data": {"expr":1000}}
]
}
}
I am writing in Python 3.5, PyMongo. 我正在用Python 3.5 PyMongo编写。
To delete one or more objects in ["user_info"]["items"] : Use $pull operator. 删除[“ user_info”] [[items“]中的一个或多个对象:使用$ pull运算符。 Example : db.collection_name.update({},{'$pull': {'user_info.items': {'id': 'id of the item'}}})
示例:db.collection_name.update({},{'$ pull':{'user_info.items':{'id':'商品的ID'}}})
To update one or more objects in ["user_info"]["items"] : Example : db.collection_name.update({'user_info.items.id':'id to change'},{'$set':{'user_info.items.$.'field to change'':'new field value'}}) 要更新[“ user_info”] [“ items”]中的一个或多个对象,例如:db.collection_name.update({'user_info.items.id':'id to change'},{'$ set':{' user_info.items。$。“要更改的字段”:“新字段值”}})
for third and fourth query, use mongo projection('$') operator.That will do your job. 对于第三和第四次查询,请使用mongo projection('$')运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.