使用 DBRef 插入 mongodb

如何使用 pymongo 将新对象插入 mongodb 并引用使用 DBRef 的其他对象。

In mongodb, external object reference is like below

{
    "external_object": {
        "$ref": "collection_where_external_object_exists",
        "$id": Object("xxxxxx")
    }
}

With bson.dbref.DBRef, above data can be posted to mongodb like below

from bson.dbref import DBRef

{
    "external_object": DBRef(collection="collection_where_external_object_exists",
    id="xxxxxx")
}

参考资料

mongodb nosql python