【问题标题】:Find all data for a specific element in my array在我的数组中查找特定元素的所有数据
【发布时间】:2015-09-10 07:42:38
【问题描述】:

我是 MongoDB 的新手。我已经将一个 json 文件导入到我的 MongoDB 中。结构如下:

我想做的是获取玩家 0、玩家 1 等的所有数据,但我不知道我的 db.collection.find 应该是什么样子。

【问题讨论】:

  • 您真正想要实现什么?您的查询应该返回什么?
  • 它应该返回 player[0] 块
  • 使用$unwindaggregation 框架。如果您可以提供示例数据集,我可能会尝试编写确切的查询。
  • 这是我的 json 文件:link

标签: mongodb robo3t


【解决方案1】:

使用db.collection.aggregate([{$unwind:"$match.players"}])。您还可以选择要投影的字段和不投影的字段。您可以将其添加到聚合管道中。

     db.collection.aggregate([{$unwind:"$match.players"},
                               {$project:{_id:1, player:'$match.players'}}
                             ]);

【讨论】:

    猜你喜欢
    • 2014-05-01
    • 2014-12-11
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多