【问题标题】:MongoDB Compass: How to select Distinct Values of a FieldMongoDB Compass:如何选择字段的不同值
【发布时间】:2020-09-16 10:04:27
【问题描述】:

我有一个 MongoDB 数据库,其中每个条目都有一个“comment”字段。我正在使用 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}

【问题讨论】:

  • 你试过了吗:$addToSet: "$comment"
  • 是的,Micki,我刚刚尝试过,但它不起作用。 “预览”仍然是空的。

标签: json mongodb field mongodb-compass unique-values


【解决方案1】:

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

预览数据应该显示在右侧

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多