MongoDb中$ ref(DBRef)的真正目的是什么 [英] what is the real purpose of $ref (DBRef) in MongoDb

查看:278
本文介绍了MongoDb中$ ref(DBRef)的真正目的是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在应用程序中使用mongo,并且在考虑设计问题时,我想到了一个问题,那么DBRef的优点/目的是什么?

I want to use mongo for my app, and while I was thinking about designing issues, I came up with question, so what are the advantages/purposes of DBRef?

例如:

> names = ['apple', 'banana', 'orange', 'peach', 'pineapple']
[ "apple", "banana", "orange", "peach", "pineapple" ]
> for (i=0; i<5; i++) {
... db.fruits.insert({_id:i, name:names[i]})
... }
> db.fruits.find()
{ "_id" : 0, "name" : "apple" }
{ "_id" : 1, "name" : "banana" }
{ "_id" : 2, "name" : "orange" }
{ "_id" : 3, "name" : "peach" }
{ "_id" : 4, "name" : "pineapple" }

我想将这些水果存储在篮子中:

and I want to store those fruits in a basket collection:

> db.basket.insert({_id:1, items:[ {$ref:'fruits', $id:1}, {$ref:'fruits', $id:3} ] })
> db.basket.insert({_id:2, items:[{fruit_id: 1}, {fruit_id: 3}]})
> db.basket.find()
{ "_id" : 1, "items" : [ DBRef("fruits", 1), DBRef("fruits", 3) ] }
{ "_id" : 2, "items" : [ { "fruit_id" : 1 }, { "fruit_id" : 3 } ] }

这两种技术之间的真正区别是什么?对我来说,使用DBRef看起来就像您只需要插入更多数据就没有任何好处....如果我错了,请更正我.

What are the real difference between those two techniques? For me it looks like using DBRef you just have to insert more data without any advantages.... Please correct me if I'm wrong.

推荐答案

基本上,DBRef是一种自我描述的ObjectID,所有驱动程序(我认为都是这样)中存在的客户端帮助程序提供了在应用程序中获取以下内容的功能:相关行.

Basically a DBRef is a self describing ObjectID which a client side helper, which exists in all drivers (I think all), provides the ability within your application to get related rows easily.

它们不是:

  • 加入
  • 级联关系
  • 服务器端关系
  • 已解决的服务器端

它们也未在Map Reduce中使用,由于分片的复杂性,该功能已被删除.

They also are not used within Map Reduce, the functionality was taken out due to complications with sharding.

尽管使用它们并不总是很好,因为与只存储ObjectID相比,如果您知道与该行相关的集合,那么它们会占用相当多的空间.不仅如此,而且由于解析方式的原因,每个相关记录都需要一张一张地被懒惰地加载,如果能够(轻松地)形成一个范围来一次查询所有相关行,那么它们就可以增加查询量您也要创建数据库,从而增加游标.

It is not always great to use these though, for one they take quite a bit of space if you know the collection that is related to that row in comparison to just storing the ObjectID. Not only that but due to how they are resolved each related record needs to be lazy loaded one by one instead if being able to form a range (easily) to query for related rows all in one go, so they can increase the amount of queries you make to the database as well, in turn increasing cursors.

这篇关于MongoDb中$ ref(DBRef)的真正目的是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆