mongodb中的dbref命令 [英] dbref command in mongodb
问题描述
我是mongodb的新手.我需要通过使用dbref引用两个集合来进行查询,并且需要查询特定字段.
I am newbie to mongodb. I need to do a query by reference two collections using dbref and need to query particular fields.
评论集合
{
uid:12345,
pid:444,
comment="blah"
},
{
uid:12345,
pid:888,
comment="asdf"
},
{
uid:99999,
pid:444,
comment="qwer"
}
用户集合
{
uid:12345,
name:"john"
},
{
uid:99999,
name:"mia"
}
我是否知道该命令如何通过在两个集合usind dbref之间添加引用来在mongodb中插入这些集合? uid中的注释必须引用用户uid.
May I know the command how to insert these collections in mongodb by adding reference between two collections usind dbref? comments in uid must refer the users uid.
推荐答案
数据库参考( DBRefs 是用于存储与其他集合相关的ID的约定,但不是MongoDB服务器支持的功能(即联接").根据您用来访问MongoDB的语言驱动程序的不同,可能会有些支持以下操作DBRefs并获取相关文档..但这确实涉及其他查询,就像您手动进行的一样.
Database References (DBRefs) are a convention for storing IDs related to other collections, but are not a feature supported by the MongoDB server (i.e. a "join"). Depending on the language driver you are using to access MongoDB, there may be some support for following DBRefs and fetching the related documents .. but this does involve additional queries, the same as if you did so manually.
因此在单独的集合中查找与用户相关的注释的伪代码如下:
So the pseudo code for finding comments related to users in separate collections is something like:
-
find({..})
个感兴趣的用户 - 对于找到的每个用户
- ...
find({uid:...})
该用户的所有评论
find({..})
users of interest- For each user found
- ...
find({uid:...})
all comments for that user
根据您的用例,您可能需要考虑嵌入信息而不是链接.例如,comments
集合实际上可能更适合嵌入在posts
集合内部(站点上的每个post
都有许多嵌入的comments
).嵌入的注释可能包含一些基本的用户信息,例如显示名称,因此您不必在users
集合中进行查找即可呈现页面.
Depending on your use case, you may want to consider embedding information rather than linking. For example, the comments
collection may actually be more appropriate embedded inside a posts
collection (each post
on the site has many embedded comments
). Embedded comments could contain some basic user information such as a display name so you do not have to look this up in the users
collection in order to render a page.
有关更多信息,请参见:
For more information see:
这篇关于mongodb中的dbref命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!