我正在尝试更新将一些数据推送到mongodb中的文档 . 以下运行正常:

import pymongo
import bson
from bson.objectid import ObjectId

client = pymongo.MongoClient('mongodb://localhost:27351/')
db = client.db
foo = db.foo

for i in range(2):
    nm = "xxx"+str(i)
    foo.insert_one({
            "foo":"bar", 
            "name": nm, 
            "friends": i, 
            "enemies": i+10, 
            "relations":
            {
                "friends": i,
                "enemies": i+10
            }
        })

然后我可以像这样添加一个ducuments:

foo.update_one({"name": "xxx1"},{"$push":{"comment":{"initial": "hello", "last": "world"}}})
list(foo.find())

它返回:

[{'_id': ObjectId('575f3b32f203c6069d9674ee'),
  'enemies': 10,
  'foo': 'bar',
  'friends': 0,
  'name': 'xxx0',
  'relations': {'enemies': 10, 'friends': 0}},
 {'_id': ObjectId('575f3b32f203c6069d9674ef'),
  'comment': [{'initial': 'hello', 'last': 'world'}],
  'enemies': 11,
  'foo': 'bar',
  'friends': 1,
  'name': 'xxx1',
  'relations': {'enemies': 11, 'friends': 1}}]

问题是以下错误 - 为什么?:

foo.update_one({"name": "xxx1"},{"$push":{"relations":{"enemies": 99, "friends": 88}}})

WriteError:字段'relations'必须是一个数组,但在文档{_id:ObjectId('575f3b32f203c6069d9674ef')}中的类型为Object