在mongodb中链接文档的语法 [英] syntax for linking documents in mongodb

查看:62
本文介绍了在mongodb中链接文档的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在用户集合中有两个对象:

If I have two objects in a user collection:

{_id: 1, name: 'foo', workItems: []}
{_id: 2, name: 'bar', workItems: []}

我如何为每个用户将指向workItem集合中对象的链接添加到workItems数组中?

how would I add links to objects in a workItem collection into the workItems array for each user?

我了解直接嵌入,但是有些workItem会分配给多个用户,所以我不想重复数据。我查看了 mongodb.org ,但是找不到任何链接示例。

I understand direct embedding but some workItems will be assigned to multiple users so I don't want to duplicate data. I have looked on mongodb.org but I can't find any examples of linking.

推荐答案

您可以执行以下DBRef:

You can do a DBRef like this:

{ $ref : <name of collection where reference is>, $id : <_id of document>, $db : <optional argument for specifying the databse the document is at> }

所以您的文档看起来像这样:

So your document would look like this:

{_id: 1, name: 'foo', workItems: {$ref: "blarg", $id: "1"}}

这篇关于在mongodb中链接文档的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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