DBRefを使用してmongodbに挿入する

DBRef を使用して他のオブジェクトを参照しながら、pymongo を使用して新しいオブジェクトを mongodb に挿入する方法。

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