简体   繁体   English

MongoDB python嵌套对象

[英]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编写。

  1. I need delete one or more objects in ["user_info"]["items"] 我需要删除[“ user_info”] [“ items”]中的一个或多个对象
  2. I need update one or more objects in ["user_info"]["items"] 我需要更新[“ user_info”] [[items“]中的一个或多个对象
  3. I need get value by value, example PLAYER_DEFAULT.find({}, {"item_type","user"}) 我需要按值获取价值,例如PLAYER_DEFAULT.find({},{“ item_type”,“ user”})
  4. How to operate with embedded objects? 如何处理嵌入式对象? -> example PLAYER_DEFAULT.find({"0.user_info.items.id":1}) < - how remove \\ update 1 element in items? ->示例PLAYER_DEFAULT.find({“ 0.user_info.items.id”:1})<-如何删除\\更新项目中的1个元素?
  1. 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'}}})

  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM