如何使用 mongodb 创建嵌入式数组列表并更新它(在 python 中)?

How to create embeded array list using pymongo and updat it (in python)?

如何在 mongodb python 中使用 pymongo 创建如下所示的 mongodb 搭配,以及如何插入新对象仅地址数组?

  {
   _id: "joe",
   name: "Joe Bookreader",
   addresses: [
                {
                  street: "123 Fake Street",
                  city: "Faketon",
                  state: "MA",
                  zip: "12345"
                },
                {
                  street: "1 Some Other Street",
                  city: "Boston",
                  state: "MA",
                  zip: "12345"
                }
              ]
 }

你需要使用$push

if (_id_exists):

        newAdrz = {"$push": {"addresses": 
                            {
                                street: "1 Some Other Street", 
                                city: "Boston",
                                state: "MA", 
                                zip: "12345"
                            }}
                   }

        self.collection.update({_id: "joe"}, newAdrz)

其他:

        root = {_id: "joe",name: "Joe Bookreader",
            addresses: [
            {
              street: "123 Fake Street",
              city: "Faketon",
              state: "MA",
              zip: "12345"
            }]
                }

          self.collection.insert(root)

如果 _id 已经存在于 mongodb

中,这将更新地址数组