通过 DBRef 数组查找文档
解决方案可能就在我面前,但我还没有找到它。我的问题是我需要查找包含指定 DBRef 的所有文档。以下是要搜索的集合的结构:
{
"_id" : ObjectId("4e2d4892580fd602eb000003"),
"date_added" : ISODate("2011-07-25T11:42:26.395Z"),
"date_updated" : ISODate("2011-07-25T11:43:09.870Z"),
...
"a_list_of_dbrefs" : [
{
"$ref" : "somecollection"
"$id" : "4e2d48ab580fd602eb000004"
}
],
...
"name" : "some name"
}
我需要能够基于 a_list_of_dbrefs
中出现的 DBRef 检索一组文档(某些 a_list_of_dbrefs
可能不包含 DBRef,其他可能包含 1,其他可能包含超过 1)。
这是如何实现的?
The solution is probably staring me in the face, but I haven't had any luck in finding it. My problem is that I need to find all documents which contain specified DBRef. Here's the structure of the collection to be searched:
{
"_id" : ObjectId("4e2d4892580fd602eb000003"),
"date_added" : ISODate("2011-07-25T11:42:26.395Z"),
"date_updated" : ISODate("2011-07-25T11:43:09.870Z"),
...
"a_list_of_dbrefs" : [
{
"$ref" : "somecollection"
"$id" : "4e2d48ab580fd602eb000004"
}
],
...
"name" : "some name"
}
I need to be able to retrieve a set of documents based on a DBRef appearing in a_list_of_dbrefs
(some a_list_of_dbrefs
may contain no DBRefs, others may contain 1, and others may contain more than 1).
How is this accomplished?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,它对我有用:
您还可以检索具有集合引用的所有元素:
Try this one, it worked for me:
You can also retrieve all elements which has the ref to collection:
我建议转储
DBRef
,而只存储引用文档的_id
(假设您知道所引用的集合的名称)。绝对没有办法在 MongoDB 上从服务器端(一步)“解析”
DBRef
数组,并且需要您循环遍历客户端上的数组并单独解析每个文档。相反,如果您存储仅包含引用的
_id
的数组,则可以检索该数组,然后使用$in
查询来获取全部内容。因此,您的文档可能会更改为如下所示:
然后您可以使用
references
字段的内容查询 MongoDB:I'd recommend dumping the
DBRef
s in favor of simply storing the_id
of the referenced document assuming you know the name of the collection being referenced.There is absolutely no way to "resolve" an array of
DBRef
from the server-side (in a single step) on MongoDB and requires that you loop through the array on the client and individually resolve each document.Conversely, if you store an array of just the referenced
_id
you can retrieve that array and then use the$in
query to fetch them all.So your document might change to look like this:
You can then query MongoDB using the contents of the
references
field: