好吧,所以使用 Python 和 MongoDB,我试图在数组中嵌入一个子文档,并在数组中使用自定义键值。我正在尝试各种不同的方法来做到这一点,但我无法弄清楚我做错了什么,所以我暂时选择了下面的工作代码。多次尝试总是导致错误:
在 _check_write_command_response raise OperationFailure(error.get("errmsg"), error.get("code"), error) pymongo.errors.OperationFailure: 'followedBy..first.rule' 中的虚线字段 'first.rule' 不是存储有效。
代码:
citizens.update(
{"_id" : userPush},
{"$push": {"followedBy":[field[1], field[2], field[3], field[0]]}})
产生:
"_id" : ObjectId("5…asfd"),
"uName" : "tim0",
"fName" : "tim",
"lName" : "lost",
"pic" : null,
"bio" : "I <3 MongoDB",
"followedBy" : [
[
"BobTheBomb",
"bobby",
"knight",
NumberInt(2)
],
[
"Robert",
"DROP",
"TABLE",
NumberInt(6)
]
这就是我要的:
"_id" : ObjectId("5…asfd"),
"uName" : "tim0",
"fName" : "tim",
"lName" : "lost",
"pic" : null,
"bio" : "I <3 MongoDB",
"followedBy" : [
"BobTheBomb": {
"fName" : "bobby",
"lName" : "knight",
"uID" : NumberInt(2)
},
"Robert": {
"fName" : " DROP ",
"lName" : " TABLE ",
"uID" : NumberInt(6)
}
]