Mongodb多表查询
var a={value:"1"}
var b={value:"2"}
var c={value:"9"}
var d={value:"10"}
db.A.save(a)
db.A.save(b)
db.A.save(c)
db.A.save(d)
db.A.find()
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed651"), "value" : "1" }
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed652"), "value" : "2" }
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed653"), "value" : "9" }
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed654"), "value" : "10" }
var Ba={Apid:[new DBRef('A',ObjectId("53ab8b45a0f7f5ae1a2ed652"))],value:3}
db.B.save(Ba)
var Ba={Apid:[new DBRef('A',ObjectId("53ab8b45a0f7f5ae1a2ed652"))],value:4}
db.B.insert(Ba)
var Ba={Apid:[new DBRef('A',ObjectId("53ab8b45a0f7f5ae1a2ed652"))],value:7}
db.B.insert(Ba)
var Ba={Apid:[new DBRef('A',ObjectId("53ab8b45a0f7f5ae1a2ed652"))],value:8}
db.B.insert(Ba)
db.B.find()
{ "_id" : ObjectId("53ab8c57a0f7f5ae1a2ed655"), "Apid" : [ DBRef("A", ObjectId("
53ab8b45a0f7f5ae1a2ed652")) ], "value" : 3 }
{ "_id" : ObjectId("53ab8c57a0f7f5ae1a2ed656"), "Apid" : [ DBRef("A", ObjectId("
53ab8b45a0f7f5ae1a2ed652")) ], "value" : 4 }
{ "_id" : ObjectId("53ab8c57a0f7f5ae1a2ed657"), "Apid" : [ DBRef("A", ObjectId("
53ab8b45a0f7f5ae1a2ed652")) ], "value" : 7 }
{ "_id" : ObjectId("53ab8c57a0f7f5ae1a2ed658"), "Apid" : [ DBRef("A", ObjectId("
53ab8b45a0f7f5ae1a2ed652")) ], "value" : 8 }
var Ca={Bpid:[new DBRef('B',ObjectId("53ab8c57a0f7f5ae1a2ed656"))],value:5}
db.C.save(Ca)
var Ca={Bpid:[new DBRef('B',ObjectId("53ab8c57a0f7f5ae1a2ed656"))],value:6}
db.C.save(Ca)
db.C.find()
{ "_id" : ObjectId("53ab8cb1a0f7f5ae1a2ed659"), "Bpid" : [ DBRef("B", ObjectId("
53ab8c57a0f7f5ae1a2ed656")) ], "value" : 5 }
{ "_id" : ObjectId("53ab8cb1a0f7f5ae1a2ed65a"), "Bpid" : [ DBRef("B", ObjectId("
53ab8c57a0f7f5ae1a2ed656")) ], "value" : 6 }
目前为止3个collection 的关系已经建成。
查询:
> var a = db.B.findOne({"value":4})
> a.Apid.forEach(function(ref){printjson(db[ref.$ref].findOne({"_id":ref.$id}));
})
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed652"), "value" : "2" }
>
> db.B.findOne().Apid[0].$id
ObjectId("53ab8b45a0f7f5ae1a2ed652")
> db.A.findOne({"_id":db.B.findOne().Apid[0].$id})
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed652"), "value" : "2" }
>
var a={value:"1"}
var b={value:"2"}
var c={value:"9"}
var d={value:"10"}
db.A.save(a)
db.A.save(b)
db.A.save(c)
db.A.save(d)
db.A.find()
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed651"), "value" : "1" }
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed652"), "value" : "2" }
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed653"), "value" : "9" }
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed654"), "value" : "10" }
var Ba={Apid:[new DBRef('A',ObjectId("53ab8b45a0f7f5ae1a2ed652"))],value:3}
db.B.save(Ba)
var Ba={Apid:[new DBRef('A',ObjectId("53ab8b45a0f7f5ae1a2ed652"))],value:4}
db.B.insert(Ba)
var Ba={Apid:[new DBRef('A',ObjectId("53ab8b45a0f7f5ae1a2ed652"))],value:7}
db.B.insert(Ba)
var Ba={Apid:[new DBRef('A',ObjectId("53ab8b45a0f7f5ae1a2ed652"))],value:8}
db.B.insert(Ba)
db.B.find()
{ "_id" : ObjectId("53ab8c57a0f7f5ae1a2ed655"), "Apid" : [ DBRef("A", ObjectId("
53ab8b45a0f7f5ae1a2ed652")) ], "value" : 3 }
{ "_id" : ObjectId("53ab8c57a0f7f5ae1a2ed656"), "Apid" : [ DBRef("A", ObjectId("
53ab8b45a0f7f5ae1a2ed652")) ], "value" : 4 }
{ "_id" : ObjectId("53ab8c57a0f7f5ae1a2ed657"), "Apid" : [ DBRef("A", ObjectId("
53ab8b45a0f7f5ae1a2ed652")) ], "value" : 7 }
{ "_id" : ObjectId("53ab8c57a0f7f5ae1a2ed658"), "Apid" : [ DBRef("A", ObjectId("
53ab8b45a0f7f5ae1a2ed652")) ], "value" : 8 }
var Ca={Bpid:[new DBRef('B',ObjectId("53ab8c57a0f7f5ae1a2ed656"))],value:5}
db.C.save(Ca)
var Ca={Bpid:[new DBRef('B',ObjectId("53ab8c57a0f7f5ae1a2ed656"))],value:6}
db.C.save(Ca)
db.C.find()
{ "_id" : ObjectId("53ab8cb1a0f7f5ae1a2ed659"), "Bpid" : [ DBRef("B", ObjectId("
53ab8c57a0f7f5ae1a2ed656")) ], "value" : 5 }
{ "_id" : ObjectId("53ab8cb1a0f7f5ae1a2ed65a"), "Bpid" : [ DBRef("B", ObjectId("
53ab8c57a0f7f5ae1a2ed656")) ], "value" : 6 }
目前为止3个collection 的关系已经建成。
查询:
> var a = db.B.findOne({"value":4})
> a.Apid.forEach(function(ref){printjson(db[ref.$ref].findOne({"_id":ref.$id}));
})
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed652"), "value" : "2" }
>
> db.B.findOne().Apid[0].$id
ObjectId("53ab8b45a0f7f5ae1a2ed652")
> db.A.findOne({"_id":db.B.findOne().Apid[0].$id})
{ "_id" : ObjectId("53ab8b45a0f7f5ae1a2ed652"), "value" : "2" }
>