如何在不使用unwind的情况下匹配mongodb中的数组字段和嵌套数组字段
我的 json 数据将如下所示,
{
"videoId": 29,
"videoComments": [
{
"comment": "awsome",
"userId": 15,
"commentTime": "2022-03-01T12:37:49.734Z",
"userName": "user1646127068323",
"deletedbyowner": "FALSE",
"_id": "621e139d8e4195079c86488",
"replyComments": [
{
"replyComment": "thank you",
"replyCommentTime": "2022-03-01T12:44:53.193Z",
"replyDeletedbyowner": "FALSE",
"_id": "621e154557fa7045e342540"
}
]
}
]
}
我需要匹配下面提到的一些条件:
- 匹配“videoId”==“29”
- 然后匹配“videoComments.deletedbyowner”==“FALSE”
- 如果我匹配第二个条件那么我需要匹配 "videoComments.replyComments.replyDeletedbyowner" == "FALSE"
我不能使用 unwind,因为我的老板告诉我 unwind 是一项成本高昂的操作,它会影响应用程序的性能。因此,在不使用 unwind 的情况下,我需要匹配这些条件。 你能帮忙解决这个问题吗?
My json data will look like this
{
"videoId": 29,
"videoComments": [
{
"comment": "awsome",
"userId": 15,
"commentTime": "2022-03-01T12:37:49.734Z",
"userName": "user1646127068323",
"deletedbyowner": "FALSE",
"_id": "621e139d8e4195079c86488",
"replyComments": [
{
"replyComment": "thank you",
"replyCommentTime": "2022-03-01T12:44:53.193Z",
"replyDeletedbyowner": "FALSE",
"_id": "621e154557fa7045e342540"
}
]
}
]
}
I need to match some conditions that I mentioned below :
- match "videoId" == "29"
- then match "videoComments.deletedbyowner" == "FALSE"
- if I match second condition then I need to match
"videoComments.replyComments.replyDeletedbyowner" == "FALSE"
I can't use unwind because my boss told me that unwind is a costly operation it will effect the app performance. so with out using unwind I need to match these conditions.
could you please help out of this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查询
videoId=29
的文档,deletedbyowner="FALSE"
的元素replyDeletedbyowner="FALSE"
的元素*这是聚合解决方案,我们还有
$elemMatch
和$
来投影匹配的元素,但这里您需要在嵌套数组上匹配,所以我认为您需要聚合,但我是不完全确定。在此处测试代码
Query
videoId=29
deletedbyowner="FALSE"
replyDeletedbyowner="FALSE"
*this is aggregate solution, we have also
$elemMatch
and$
to project the matched element, but here you need match on nested arrays, so i think you need aggregation, but i am not completly sure.Test code here
我想这就是您正在寻找的。有关更多信息,请查看此doc
I think this is what you are looking for. For more info check this doc