放松后MongoDB组文件
在我的mongodb books
集合中,我的文档看起来像:
{
_id: ObjectId(625efa44f1ba751c8275ea51),
contributors:[ObjectId(625efa44f1ba751c8275ea52), ObjectId(625efa44f1ba751c8275ea53)]
//other fields
}
我想做一个查询,以返回我的文档,例如:
{
_id: ObjectId(625efa44f1ba751c8275ea51),
contributors:[
{
_id: ObjectId(625efa44f1ba751c8275ea52),
first_name: 'Luigi'
//many other fields
},
{
_id: ObjectId(625efa44f1ba751c8275ea53),
first_name: 'Mario'
//many other fields
},
]
//other fields
}
我在贡献者上做了undind
查找使用我的用户
集合,现在我需要对它们进行分组。我以前从未使用过它,但是我做了类似的事情:
{
$group:{
_id: '_id'
}
}
但是我不知道下一步该怎么做以保留书籍和用户的所有字段。
你有什么想法吗?
In my mongodb books
collection I have documents that look like:
{
_id: ObjectId(625efa44f1ba751c8275ea51),
contributors:[ObjectId(625efa44f1ba751c8275ea52), ObjectId(625efa44f1ba751c8275ea53)]
//other fields
}
And I want to do a query that returns me documents like:
{
_id: ObjectId(625efa44f1ba751c8275ea51),
contributors:[
{
_id: ObjectId(625efa44f1ba751c8275ea52),
first_name: 'Luigi'
//many other fields
},
{
_id: ObjectId(625efa44f1ba751c8275ea53),
first_name: 'Mario'
//many other fields
},
]
//other fields
}
I did an unwind
on contributors and a lookup
with my users
collection and now I need to group them. I haven't used it before but I did something like:
{
$group:{
_id: '_id'
}
}
But I don't know what to do next in order to preserve all the fields from books and also from users.
Do you have any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
$查找
结果足够小(< 16mb 文档尺寸限制),您可以简单地做
$查找
。这是 mongo playground 供您参考。
如果
$查找
结果将超过16 MB的限制,则您仍然可以$ utind。只需使用
$ first在
$ untind之后重组其他字段。这是蒙哥游乐场供您参考。
If the
$lookup
result is small enough (<16MB document size limit),you can simply do a
$lookup
.Here is the Mongo Playground for your reference.
If the
$lookup
result will exceed 16 MB limit, you can still$unwind. Just use
$firstto regroup the other fields after the
$unwind`Here is the Mongo playground for your reference.