日期:2014-05-16  浏览次数:20428 次

MongoDB Middle Level (关联多表查询)

DBRef is a more formal specification for creating references between documents.? DBRefs (generally) include a collection name as well as an object id.? Most developers only use DBRefs if the collection can change from one document to the next.? If your referenced collection will always be the same, the manual references outlined above are more efficient.

?

[sql] view plaincopyprint?
^_^[root@:/usr/local/mongodb/bin]#./mongo
MongoDB shell version: 1.8.2
connecting to: test
> var a = {name:"C++"}                                                                                           
> db 
test
> db.language.save(a)
> db.language.find()
{ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }
> var b = {name:"javascript"}
> db.language.save(b)
> db.language.find()
{ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }
{ "_id" : ObjectId("4da32cb17d2de864e0448e07"), "name" : "javascript" }
> lan = {name:"obj1",computer:[new DBRef('language',a._id)]}
{
        "name" : "obj1",
        "computer" : [
                {
                        "$ref" : "language",
                        "$id" : ObjectId("4da32c897d2de864e0448e06")
                }
        ]
}
> lan.computer[0]
{ "$ref" : "language", "$id" : ObjectId("4da32c897d2de864e0448e06") }
> lan.computer[0].fetch()
{ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }
> db.language.insert(lan)                                                                                        
> db.language.find()
{ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }
{ "_id" : ObjectId("4da32cb17d2de864e0448e07"), "name" : "javascript" }
{ "_id" : ObjectId("4da33b487d2de864e0448e08"), "name" : "obj1", "computer" : [ { "$ref" : "language", "$id" : ObjectId("4da32c897d2de864e0448e06") } ] }
> db.language.findOne({name:"obj1"}).computer[0].fetch()                                                         
{ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }
> lan2 = {name:"obj2",computer:[new DBRef('language',b._id)]}     
{
        "name" : "obj2",
        "computer" : [
                {
                        "$ref" : "language",
                        "$id" : ObjectId("4da32cb17d2de864e0448e07")
                }
        ]
}
> db.language.insert(lan2)
> db.language.find()
{ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }
{ "_id" : ObjectId("4da32cb17d2de864e0448e07"), "name" : "javascript" }
{ "_id" : ObjectId("4da33b487d2de864e0448e08"), "name" : "obj1", "computer" : [ { "$ref" : "language", "$id" : ObjectId("4da32c897d2de864e0448e06") } ] }
?

?

?

相关数据存放在一起,针对性的查询可以消除join,性能比分散存储要高且方便

?

?

在关系型数据库中,通过连接运算符可以实现多个表联合查询。而非关系型数据库的特点是表之间属于弱关联,Mongodb作为Nosql代表,其本身特性不建议对多Collection关联处理,不过对于有些需要对多表关联处理的需求,Mongodb也可以实现。主要分为两种方式:简单手工关联和DBRef方式关联

?

1.简单手工关联

下图表示帖子和用户两个Collection的ER图:

首先将authors集合中的用户对象查询出来,放在一个变量author中,代码如下:

Shell代码