3

我有一个 MongoDB 数据库,其中每个条目都有一个“评论”字段。我正在使用MongoDB Compass连接到它,并试图找到“评论”字段的所有唯一值。我在 Compass 的“聚合”选项卡中尝试了以下代码(见下图),但没有得到任何结果:

{$group: {
  _id: null,
  unique_comments: {
    $addToSet: comment
  }
}

JSON格式的数据示例如下:

{"name":"Hannah Maxwell","date":"2020-01-07T20:16:13.931629-08:00","comment":"Maybe I like camping","location":{"latitude":43.81710433959961,"longitude":-79.42430114746094,"country":"CA","region":"ON"},"likes":1,"responses":24}
{"name":"Jordan Rodriguez","date":"2019-04-28T20:16:13.931629-07:00","comment":"I hate skiing","location":{"latitude":43.41518783569336,"longitude":-80.51475524902344,"country":"CA","region":"ON"},"likes":5,"responses":1}
{"name":"Bodhi Mckenzie","date":"2019-07-09T20:16:13.931629-07:00","comment":"I think I might like skiing","location":{"latitude":43.65549087524414,"longitude":-79.38555908203125,"country":"CA","region":"ON"},"likes":65,"responses":5}

MongoDB 指南针聚合选项卡的图像

4

1 回答 1

3

在 MongoDB Compass 中,您需要从菜单中选择聚合管道阶段,尝试:

在此处输入图像描述

预览数据应显示在右侧

于 2020-05-28T23:24:06.283 回答