MongoDB 指南针:如何 select 字段的不同值

MongoDB Compass: How to select Distinct Values of a Field

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

{$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 Compass 中,您需要从菜单中选择聚合管道阶段,尝试:

预览数据应该显示在右侧