-1

さて、Python と MongoDB を使用して、配列内にカスタム キー値を持つサブドキュメントを配列内に埋め込もうとしています。私はこれを行うためにあらゆる種類のさまざまな方法をいじっていましたが、何が間違っているのか理解できなかったため、一時的に以下の作業コードに落ち着きました. 何度も試行すると、常にエラーが発生します。

in _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)
        }
    ]
4

1 に答える 1

0

そのデータ構造を構築する必要があります。現在、それ"followedBy"はリストにすぎないと言っています。

だから試してください:

citizens.update(
    {"_id" : userPush},
    {"$push": {"followedBy":{field[1]: { "fName":field[2], "lName":field[3], "uID":field[0]}}}})

リストを削除し、dict に置き換えます。

これが役立つことを願っています。


私はあなたに有効なを与えていないことに気付きました、私jsonはこれをテストしました:

citizens.update(
    {"_id" : userPush},
    {$push: 
    {"followedBy":
        [
            {field[1]: 
                { "fName": field[2], "lName": field[3], "uID": field[0]}
            }
        ]
    } 
})

そしてそれは働いた...


使用している修飾子が原因でエラーが発生する場合があります。ブログで次のことを見つけました。

MongoDB には、ドキュメントをその場で更新するために使用できるいくつかの異なる修飾子が用意されています(詳細については、 update を参照してください)。

- $inc Increment a numeric field (generalized; can increment by any number)
- $set Set certain fields to new values
- $unset Remove a field from the document
- $push Append a value onto an array in the document
- $pushAll Append several values onto an array
- $addToSet Add a value to an array if and only if it does not already exist
- $pop Remove the last (or first) value of an array
- $pull Remove all occurrences of a value from an array
- $pullAll Remove all occurrences of any of a set of values from an array
- $rename Rename a field
- $bit Bitwise updates

むしろ使用したいアイテムをたくさん挿入しているため、それが見つかるかもしれません$pushAll...ただの憶測です...$addToSet$push

于 2015-03-12T07:42:19.403 に答える