小编dro*_*lli的帖子

MongoDB Compass:如何选择字段的不同值

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

{$group: {
  _id: null,
  unique_comments: {
    $addToSet: comment
  }
}
Run Code Online (Sandbox Code Playgroud)

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}
Run Code Online (Sandbox Code Playgroud)

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

json field mongodb unique-values mongodb-compass

3
推荐指数
1
解决办法
9732
查看次数

标签 统计

field ×1

json ×1

mongodb ×1

mongodb-compass ×1

unique-values ×1