Mongoose连接数据
如果我的 MongoDB 中有一个对象需要在我的系统中的任何地方使用,那么它就在它自己的集合中。但是,我不太清楚如何让数据自动显示在它所连接的其他对象上。
这是一个示例:
Schema1 = { name: String }
Schema2 = { something: String, other_thing: [{schema1_id: String}] }
现在我想要的是能够说 var name = mySchema2.name;
并获取链接的 Schema1
对象的名称。
我正在使用 Mongoose、Express 和 Node.js,并且我尝试使用 Mongoose“虚拟”来实现此目的,但是当我说 res.send(myobject);
时,我在任何地方都看不到虚拟属性在物体上。
最好的方法是什么?
If I have an object in my MongoDB that will need to be used EVERYWHERE in my system, so it is in its own collection. However, I con't quite figure out how to get the data to show up automatically on the other objects it is joined to.
Here is an example:
Schema1 = { name: String }
Schema2 = { something: String, other_thing: [{schema1_id: String}] }
Now what I want is to be able to say var name = mySchema2.name;
and get the name of the linked Schema1
object.
I am using Mongoose, Express and Node.js and I have tried using a Mongoose 'virtual' for this, but when I say res.send(myobject);
I don't see the virtual property anywhere on the object.
What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 mongodb 中没有JOIN这样的词,因为连接破坏了可扩展性。但大多数驱动程序都支持 DBRef,它们只是发出额外的请求来加载引用的数据。
因此,您可以自己提出额外的请求来加载您在任何地方使用的对象。
如果您在应用程序中的任何地方都使用某个对象,那么它听起来像是需要在缓存中的对象。但如果有足够的内存将对象加载到内存中,mongodb 就会充当某种缓存。因此,为了简单起见,只需发出额外的请求来加载对象即可。
In mongodb no such word as JOIN, because of joins killing scalability. But most drivers support DBRefs, they are just making additional request to load referenced data.
So you can just make additional request yourself to load object that you using everywhere.
If you using some object everywhere in your app it sounds like object that need to be in cache. But mongodb work as some kind o cache if enough memory to load object into memory. So, to keep it simple just make additional request to load object.
我知道你发布问题已经很久了,但它可能对其他人有帮助。
如果您一直使用此参考,您可能需要考虑使用嵌入式文档。嵌入文档的好处是,您可以在查询父文档时获取它们,从而节省额外的查询,缺点是父文档可能会变得很大(甚至非常大),因此您应该使用它们,但要小心使用它们。< br>
这是一个简单的嵌入文档的示例。我们将嵌入它(代码有点伪),而不是在帖子文档中引用“评论”(这需要额外的查询):
MongoDB 允许您以简单方便的方式通过点字符查询评论字段。例如,如果我们只想查询主题以“car”开头的评论,我们将执行以下操作:
请注意,为了示例简单起见,帖子中的评论字段不是数组(本示例中允许每个帖子一条评论)。然而,即使它是一个数组,mongo 也会以同样的方式非常优雅地引用数组的元素。
I know it is far after you post the question but it might help others.
If you use this reference all over you may want to consider using embedded document. The benefits of embedded document is that you get them when you query the parent document thus it save you additional query and the drawbacks is that the parent document may become large (or even very large) thus you should use them but use them carefully.
Here is an example of simple embedded document. Instead of referencing 'comments' in the post document, which require additional query, we will embed it (code is a bit pseudo):
MongoDB allows you a simple and convenience way to query comments' fields by the dot character. For example if we like to query only comments which their subject starts with 'car' we do as follow:
Note that for simplicity of the example the comment field in the post is not an array (one comment per post is allowed in this example). However even if it will be an array, mongo refer to array's elements very elegantly in the same way.
有几个插件可以帮助 Mongoose 中的 DBRefs。
mongoose-dbref 使用 DBRef 标准,可能是一个不错的起点。
mongoose-plugins 是我不久前写的,但它的工作方式略有不同。
There are a couple of plugins to help with DBRefs in Mongoose.
mongoose-dbref uses the DBRef standards and is probably a good place to start.
mongoose-plugins is one I wrote a while ago but it works in a slightly different way.