mongoDB中的DBRef
来源:互联网 发布:mysql 改变表名 编辑:程序博客网 时间:2024/05/14 19:27
前段时间串讲的时候mongoDB中的DBRef这个东西没有讲太明白,由于时间原因就匆匆带过了。在这里附上一个小实验,补充一下。
As MongoDB is non-relational (no joins), references (”foreign keys”) between documents are generally resolved client-side by additional queries to the server.
A DBRef is a reference from one document (object) to another within a database.
个人理解DBRef就是在两个collection之间定义的一个关联,比如把collectionB的”_id”列的值存在collectionA的一个列中,然后通过collectionA这个列中所存的值在collectionB中找到相应的记录。
mongoDB号称是Document-oriented database。在mongoDB中,collection就相当于table,document相当于row,至于上面提到的object,可以理解 为一个行对象,当存储进一个collection中就变成一条document。而所谓的”_id”列其实就是系统自动生成的一个唯一列,用于标识一个 collection里的每条document。系统默认会对”_id”列进行索引,如果可以的话,你可以指定RDBMS表中的主键列来填充”_id”, 这样既保证了唯一性,也能有效的利用起这个索引。
下面是一个关于课程表和学生表的例子,学生表有”_id”,name,classes三个列,课程表有”_id”,name两个列。通过在学生表的classes列中存储课程表中相应记录的”_id”值,来达到两张表关联的目的。
MongoDB shell version: 1.6.0
connecting to: test
> c = {name:’English’} #define a course object
{ “name” : “English” }
> db.courses.save(c) #save this object into a collection named courses
> db.courses.find() #use find() to retrive the documents in colletion courses,just like the select in RDBMS
{ “_id” : ObjectId(”4c7ce443e7140000000072c1″), “name” : “English” }
> db.courses.insert({name:”Biology”}) #insert another document into collection courses
> db.courses.find()
{ “_id” : ObjectId(”4c7ce443e7140000000072c1″), “name” : “English” }
{ “_id” : ObjectId(”4c7ce498e7140000000072c2″), “name” : “Biology” }
> stu1 = {name:”Joe1″,classes:[new DBRef('courses',c._id)]} #define a student object that references the collection courses,with the column “_id”
{
“name” : “Joe1″,
“classes” : [
{
"$ref" : "courses",
"$id" : ObjectId("4c7ce443e7140000000072c1")
}
]
}
> stu1.classes[0]
{ “$ref” : “courses”, “$id” : ObjectId(”4c7ce443e7140000000072c1″) }
> stu1.classes[0].fetch() #use the object stu1 to fetch the value referenced in collection courses
{ “_id” : ObjectId(”4c7ce443e7140000000072c1″), “name” : “English” }
> db.students.insert(stu1) #save the object stu1 into collection students
> db.students.find()
{ “_id” : ObjectId(”4c7ce6c0e7140000000072c4″), “name” : “Joe1″, “classes” : [ {
"$ref" : "courses", "$id" : ObjectId("4c7ce443e7140000000072c1") } ] }
> db.students.findOne({name:”Joe1″}).classes[0]
{ “$ref” : “courses”, “$id” : ObjectId(”4c7ce443e7140000000072c1″) }
> db.students.findOne({name:”Joe1″}).classes[0].fetch() #use the object stu1 to fetch the value referenced in collection courses
{ “_id” : ObjectId(”4c7ce443e7140000000072c1″), “name” : “English” }
> stu2 = {name:”Joe2″,classes:[new DBRef('courses',ObjectId("4c7ce498e7140000000072c2"))]} #define another student object stu2,also with the column “_id”,just in another style
{
“name” : “Joe2″,
“classes” : [
{
"$ref" : "courses",
"$id" : ObjectId("4c7ce498e7140000000072c2")
}
]
}
> db.students.insert(stu2)
> db.students.find()
{ “_id” : ObjectId(”4c7ce6c0e7140000000072c4″), “name” : “Joe1″, “classes” : [ {
"$ref" : "courses", "$id" : ObjectId("4c7ce443e7140000000072c1") } ] }
{ “_id” : ObjectId(”4c7ce754e7140000000072c5″), “name” : “Joe2″, “classes” : [ {
"$ref" : "courses", "$id" : ObjectId("4c7ce498e7140000000072c2") } ] }
> db.students.findOne({name:”Joe2″}).classes[0]
{ “$ref” : “courses”, “$id” : ObjectId(”4c7ce498e7140000000072c2″) }
> db.students.findOne({name:”Joe2″}).classes[0].fetch() #also works
{ “_id” : ObjectId(”4c7ce498e7140000000072c2″), “name” : “Biology” }
下面链接是官网上关于DBRef的讲解,有兴趣可以看一下
http://www.mongodb.org/display/DOCS/Database+References
- mongoDB中的DBRef
- mongodb数据库引用DBRef
- spring-mongodb-DBRef的运用
- MongoDB 通过 DBRef关联查询
- Spring Data Mongodb DBRef联级存储
- 利用DBREF实现MongoDB的引用("外键")
- Mongodb aggregate group DBRef类型,并处理返回的DBRef数据Demo
- MongoDB之DBref(关联插入,查询,删除) 实例深入
- NoSql的多表关联----Mongodb的DBRef的运用
- MongoDB之DBref(关联插入,查询,删除) 实例深入
- spring-data-mongodb 对象关联 @DBRef学习 之疑问
- MongoDB之DBref(关联插入,查询,删除) 实例深入
- mongodb 多表关联的实现和DBRef理解,
- MongoDB DBRef和手动引用(Manual References)
- @DBRef---------------springMVC
- Mongo DBRef 存储
- mgo中DBRef的使用方法
- 5.MongoDB中的索引。
- 利用Word 中的“从任意文件中恢复文本”转换器来恢复损坏的文档
- Ubuntu安装Tweak
- Fedora 13
- getsockopt()
- 第三篇
- mongoDB中的DBRef
- 一个月的数据结构小结
- Tomcat开发技术之与HTTP服务器的集成 仅供了解
- SqlCommandBuilder 对象
- Tomcat5.5中配置JNDI DataSource
- form中只有一个input元素时按回车默认提交表单
- ioctlsocket()
- SQL Server 2005 体系结构
- 谷歌搜索引擎技术