MongoDB之DBref(关联插入,查询,删除) 实例深入

释放双眼,带上耳机,听听看~!

http://blog.csdn.net/crazyjixiang/article/details/6668288

suppose I have the following datastructure:


1
2
3
4
1var user = {_id: 'foo', age: 35};
2var post = {_id: '...', author: {$ref: user, $id: 'foo'},...};
3
4

How can I query all posts which references user[foo]? I tried the following but not work:


1
2
1db.post.find('author.$id': 'foo')
2

如图所示,A,B,C三个Collection互相关联。 其中的数字为document的value值。

关于DBref的入门可以看 http://www.aiuxian.com/article/p-563964.html 这篇文章。

我们先建立A collection。

[cpp]  [http://www.aiuxian.com/article/p-563974.html]( "view plain") http://www.aiuxian.com/article/p-563974.html http://www.aiuxian.com/article/p-563974.html http://www.aiuxian.com/article/p-563974.html

 var a={value:

"1"
}  
1.

 var b={value:

"2"
}  
1.

 var c={value:

"9"
}  
1.

 var d={value:

"10"
}  
1.

 db.A.save(a)  

 db.A.save(b)        

 db.A.save(c)   

 db.A.save(d)  

 db.A.find()                                                                                                 


"_id"
 : ObjectId(
"4e3f33ab6266b5845052c02b"
), 
"value"
 : 
"1"
 }  
1.

"_id"
 : ObjectId(
"4e3f33de6266b5845052c02c"
), 
"value"
 : 
"2"
 }  
1.

"_id"
 : ObjectId(
"4e3f33e06266b5845052c02d"
), 
"value"
 : 
"9"
 }  
1.

"_id"
 : ObjectId(
"4e3f33e26266b5845052c02e"
), 
"value"
 : 
"10"
 }  

B collection以A collection的  _id为
ObjectId("4e3f33de6266b5845052c02c")作为Apid

所以:

[cpp]  [http://www.aiuxian.com/article/p-563974.html]( "view plain") http://www.aiuxian.com/article/p-563974.html http://www.aiuxian.com/article/p-563974.html http://www.aiuxian.com/article/p-563974.html

 var Ba={Apid:[

new
 DBRef(
'A'
,ObjectId(
"4e3f33de6266b5845052c02c"
))],value:3}                        
1.

 db.B.save(Ba)  

 var Ba={Apid:[

new
 DBRef(
'A'
,ObjectId(
"4e3f33de6266b5845052c02c"
))],value:4}  
1.

 db.B.insert(Ba)                                                              

 var Ba={Apid:[

new
 DBRef(
'A'
,ObjectId(
"4e3f33de6266b5845052c02c"
))],value:7}  
1.

 db.B.insert(Ba)                                                              

 var Ba={Apid:[

new
 DBRef(
'A'
,ObjectId(
"4e3f33de6266b5845052c02c"
))],value:8}  
1.

 db.B.insert(Ba)                                                              

 db.B.find()  


"_id"
 : ObjectId(
"4e3f3dd96266b5845052c035"
), 
"Apid"
 : [ { 
"$ref"
 : 
"A"

"$id"
 : ObjectId(
"4e3f33de6266b5845052c02c"
) } ], 
"value"
 : 3 }  
1.

"_id"
 : ObjectId(
"4e3f3de16266b5845052c036"
), 
"Apid"
 : [ { 
"$ref"
 : 
"A"

"$id"
 : ObjectId(
"4e3f33de6266b5845052c02c"
) } ], 
"value"
 : 4 }  
1.

"_id"
 : ObjectId(
"4e3f3dec6266b5845052c037"
), 
"Apid"
 : [ { 
"$ref"
 : 
"A"

"$id"
 : ObjectId(
"4e3f33de6266b5845052c02c"
) } ], 
"value"
 : 7 }  
1.

"_id"
 : ObjectId(
"4e3f3df06266b5845052c038"
), 
"Apid"
 : [ { 
"$ref"
 : 
"A"

"$id"
 : ObjectId(
"4e3f33de6266b5845052c02c"
) } ], 
"value"
 : 8 }  

C collection以B collection的  _id为 
ObjectId("4e3f3de16266b5845052c036") 作为Apid

[cpp]  [http://www.aiuxian.com/article/p-563974.html]( "view plain") http://www.aiuxian.com/article/p-563974.html http://www.aiuxian.com/article/p-563974.html http://www.aiuxian.com/article/p-563974.html

 var Ca={Bpid:[

new
 DBRef(
'B'
,ObjectId(
"4e3f3de16266b5845052c036"
))],value:5}                        
1.

 db.C.save(Ca)                                                                

 var Ca={Bpid:[

new
 DBRef(
'B'
,ObjectId(
"4e3f3de16266b5845052c036"
))],value:6}  
1.

 db.C.save(Ca)                                                                

 db.C.find()  


"_id"
 : ObjectId(
"4e3f42f36266b5845052c03d"
), 
"Bpid"
 : [ { 
"$ref"
 : 
"B"

"$id"
 : ObjectId(
"4e3f3de16266b5845052c036"
) } ], 
"value"
 : 5 }  
1.

"_id"
 : ObjectId(
"4e3f42f96266b5845052c03e"
), 
"Bpid"
 : [ { 
"$ref"
 : 
"B"

"$id"
 : ObjectId(
"4e3f3de16266b5845052c036"
) } ], 
"value"
 : 6 }  

目前为止3个collection 的关系已经建成。

查询


[cpp]  [http://www.aiuxian.com/article/p-563974.html]( "view plain") http://www.aiuxian.com/article/p-563974.html http://www.aiuxian.com/article/p-563974.html http://www.aiuxian.com/article/p-563974.html

<span style=
"font-size:16px;"

 var a = db.B.findOne({

"value"
:4})                                                       
1.

 a.Apid.forEach(function(ref){printjson(db[ref.$ref].findOne({

"_id"
:ref.$id}));})  
1.

"_id"
 : ObjectId(
"4e3f33de6266b5845052c02c"
), 
"value"
 : 
"2"
 }</span>  

[cpp]  [http://www.aiuxian.com/article/p-563974.html]( "view plain") http://www.aiuxian.com/article/p-563974.html http://www.aiuxian.com/article/p-563974.html http://www.aiuxian.com/article/p-563974.html

 db.A.findOne({

"_id"
:db.B.findOne().Apid[0].$id})  
1.

"_id"
 : ObjectId(
"4e3f33de6266b5845052c02c"
), 
"value"
 : 
"2"
 }  

其实好好想想引用不是必须的。
MongoDB 权威指南说了这么一句:

In short,the best time to use DBRefs are when you're storing heterogeneous references to documents in different collections.like when you want to take advantage of some additional DBRef-specific functionality in a driver or tool.

给TA打赏
共{{data.count}}人
人已打赏
安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索