简体   繁体   English

pymongo嵌套嵌入式文档字段更新

[英]pymongo nested embedded document field update

I have a document as: 我有一个文档为:

{
    "name": "restaurant 1",
    "rooms":
        [
            {"name": "room1",
                "desks": [
                    {
                        "name": "desk1",
                        "unique": "abcde",
                        "busy": False
                    },
                    {
                        "name": "desk2",
                        "unique": "abcdf",
                        "busy": True
                    }
                ]},
            {"name": "room2",
                "desks": [
                    {
                        "name": "desk1",
                        "unique": "bbcde",
                        "busy": False
                    },
                    {
                        "name": "desk2",
                        "unique": "bbcdf",
                        "busy": False
                    }
                ]}
        ]
}

My pymongo search query: 我的pymongo搜索查询:

db.restaurants.update(
  {'rooms.desks.unique': 'bbcdf')},
  {'$set': {'rooms.$.desks.$$.busy': True}}
)

I couldn't update "busy" field of the desk. 我无法更新办公桌的“忙碌”字段。 $$ part didn't work. $$部分无效。 What should I replace "$$" with? 我应该用“ $$”代替什么?

or 要么

How can I find the index of the desk. 我如何找到桌子的索引。

Thanks in advance 提前致谢

According to documentation it's not possible: 根据文档,这是不可能的:

The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value. 位置$运算符不能用于遍历一个以上数组的查询,例如遍历嵌套在其他数组中的数组的查询,因为$占位符的替换是单个值。

Most likely you will need to redesign your database schema. 您很可能需要重新设计数据库架构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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