【问题标题】:Mongo $and query returning no resultsMongo $and 查询不返回任何结果
【发布时间】:2015-06-03 14:34:34
【问题描述】:

如何编写查询以返回匹配以下集合的 match.id 和 player.name 的所有记录?

{
    "match": {
        "id": 1,
        "event": {
            "timestamp": "2015-06-03 15:02:22",
            "event": "round_stats",
            "round": 1,
            "player": {
                "name": "Jim",
                "userId": 45,
                "uniqueId": "BOT",
                "team": 2
            },
            "shots": 0,
            "hits": 0,
            "kills": 0,
            "headshots": 0,
            "tks": 0,
            "damage": 0,
            "assists": 0,
            "assists_tk": 0,
            "deaths": 0,
            "head": 0,
            "chest": 0,
            "stomach": 0,
            "leftArm": 0,
            "rightArm": 0,
            "leftLeg": 0,
            "rightLeg": 0,
            "generic": 0
        }
    }
}

我已经尝试使用以下两个查询语句,但都没有运气——它们都没有返回任何结果:

db.warmod_events.find( { $and: [ { "match.id": 1}, { "player.name": 'Jim' } ] } )

db.warmod_events.find( { $and: [ { "match.id": 1}, { "event": { "player.name": "Jim" } } ] } )

我对 Mongo 还很陌生,任何指导和解释都会有所帮助——说实话,我选择在这个项目中使用 Mongo,因为我正在使用的数据已经以这种形式(JSON)呈现,并且,因此,这似乎是一个使用和学习 Mongo 的好机会。

我目前指的是 Mongo 网站上的文档。

谢谢大家

【问题讨论】:

    标签: json mongodb mongodb-query


    【解决方案1】:

    尝试以下查询:

    db.warmod_events.find({ "match.id": 1, "match.event.player.name": 'Jim' })
    

    这将匹配匹配 id 与嵌入文档播放器名称相同的文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      相关资源
      最近更新 更多