使用 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