1、被引用数据
db.address.insert({"city":"shanghai","street":"huaihai road","no":"101"}) db.address.insert({"city":"beijing","street":"taipingqiao road","no":"102"})
2、引用数据
db.persons.insert({"name":"joe","address":{"$ref":"address","$id": ObjectId("55f522e96811e30fd403e83d"),"$db": "test"},"age":20,"sex":"male"}) db.persons.insert({"name":"leo","address":{"$ref":"address","$id": ObjectId("55f522f46811e30fd403e83e"),"$db": "test"},"age":21,"sex":"male"})
3、查询被引用数据
var user = db.persons.findOne({"name":"joe"}) var addressRef = user.address db[addressRef.$ref].findOne({"_id":(addressRef.$id)})