why pymongo/motor framework update_many method update fields value with the aggregate dict as nested document

23 Views Asked by At

the following scripts can be execute correctly in mongosh, but when execute with motor it will update fields to nested document.

origin_children_ancestors = [ObjectId("65acdb2f025de11c4b6f4fa8"),ObjectId("65ad037edbe9cca114dcaf38")]
new_children_ancestors = [ObjectId("65acdb2f025de11c4b6f4fa8"),ObjectId("65ad037edbe9cca114dcaf38"), ObjectId(""65ae10c1652cea51d101c0c1"")]

await db.categories.update_many({
    f"ancestors.{i}": val for i, val in enumerate(origin_children_ancestors)
}, {
    "$set": {
        "ancestors": {
            "$concatArrays": [
                new_children_ancestors,
                {"$slice": ["$ancestors", len(origin_children_ancestors), {"$size": "$ancestors"}]}
            ]
        }
    }
})

in mongosh the scripts updated the "ancestors" correctly, but with pymongo/motor framework, the "ancestors" is updated as:

"$concatArrays": [
  [ObjectId("65acdb2f025de11c4b6f4fa8"),ObjectId("65ad037edbe9cca114dcaf38"), ObjectId(""65ae10c1652cea51d101c0c1"")],
  {"$slice": ["$ancestors", 2, {"$size": "$ancestors"}]}
]
0

There are 0 best solutions below