go - 如何在json中按深层排序在mongo中获取20个文档?

标签 go mongodb-query

我在MongoDB中的文档:

{
  "_id": {
    "$oid": "5e90276937b54857689435733"
  },
  "user_id": "",
  "nickname": "",
  "platform": ""
  "invites": {
    "$numberInt": ""
  },
  "hide_game_stats": false,
  "game_data": {
    "player_info": {
      "level": {
        "value": {
          "$numberInt": "96"
        },
        "rank_icon_url": ""
      },
      "platform": "battle"
    },
    "warzone": {
      "battle_royale": {
        "timePlayed": {
          "$numberInt": "55789"
        },
        "score": {
          "value": {
            "$numberDouble": "202485"
          }
        },
        "kd_ratio": {
          "value": {
            "$numberDouble": "1.71"
          }
        },
        "kills": {
          "value": {
            "$numberInt": "217"
          }
        },
        "games_played": {
          "value": {
            "$numberInt": "43"
          }
        },
        "score_per_minute": {
          "value": {
            "$numberDouble": "217.77"
          }
        },
        "contracts": {
          "value": {
            "$numberInt": "41"
          }
        },
        "wins": {
          "value": {
            "$numberInt": "2"
          }
        },
        "top_5": {
          "value": {
            "$numberInt": "7"
          }
        },
        "top_10": {
          "value": {
            "$numberInt": "15"
          }
        },
        "top_25": {
          "value": {
            "$numberInt": "39"
          }
        },
        "win_rate": {
          "value": {
            "$numberDouble": "4.7"
          }
        }
      },
      "plunder": {
        "timePlayed": {
          "$numberInt": "18526"
        },
        "cash": {
          "value": {
            "$numberInt": "1158"
          }
        },
        "score": {
          "value": {
            "$numberDouble": "67551"
          }
        },
        "kd_ratio": {
          "value": {
            "$numberDouble": "1.99"
          }
        },
        "kills": {
          "value": {
            "$numberInt": "155"
          }
        },
        "games_played": {
          "value": {
            "$numberInt": "14"
          }
        },
        "score_per_minute": {
          "value": {
            "$numberDouble": "218.78"
          }
        },
        "contracts": {
          "value": {
            "$numberInt": "23"
          }
        },
        "wins": {
          "value": {
            "$numberInt": "1"
          }
        },
        "top_5": {
          "value": {
            "$numberInt": "1"
          }
        },
        "top_10": {
          "value": {
            "$numberInt": "1"
          }
        },
        "top_25": {
          "value": {
            "$numberInt": "1"
          }
        },
        "win_rate": {
          "value": {
            "$numberDouble": "7.1"
          }
        }
      }
    },
    "multiplayer": {
      "timePlayed": {
        "$numberInt": "180588"
      },
      "career_score": {
        "value": {
          "$numberDouble": "691843"
        }
      },
      "kd_ratio": {
        "value": {
          "$numberDouble": "1.23"
        }
      },
      "kills": {
        "value": {
          "$numberInt": "5345"
        }
      },
      "total_games_player": {
        "value": {
          "$numberInt": "370"
        }
      },
      "score_per_minute": {
        "value": {
          "$numberDouble": "229.86"
        }
      },
      "win_rate": {
        "value": {
          "$numberDouble": "49.7"
        }
      }
    }
  }
}

我只需要按game_data.warzone.battle_royale.kills.value排序的20个文档

请告诉我,我该怎么做

我的尝试:
findOptions := options.Find()

    findOptions.SetSort(
        bson.D{
            primitive.E{
                Key: "game_data",
                Value: bson.D{
                    primitive.E{
                        Key:   "warzone",
                        Value: bson.D{
                            primitive.E{
                                Key:   "battle_royale",
                                Value: bson.D{
                                    primitive.E{
                                        Key:   "kills",
                                        Value: bson.D{
                                            primitive.E{
                                                Key:   "value",
                                                Value: -1,
                                            },
                                        },
                                    },
                                },
                            },
                        },
                    },
                },
            },
        },
    ) // set the sort function (attempt №1)

    findOptions.SetSort(
        bson.M{
            "$match": bson.M{
                "game_data.warzone.battle_royale.kills.value": -1,
            },
        },
    ) // set the sort function (attempt №2)

    findOptions.SetLimit(20) // set limit to 20 docs
    cursor, err := mongodb.CodHubCollection.Find(context.Background(), bson.D{}, findOptions)
    if err != nil {
        return err
    }

最佳答案

您不会在排序中使用$ match操作:

findOptions.SetSort(bson.D{{"game_data.warzone.battle_royale.kills.value", -1}})

关于go - 如何在json中按深层排序在mongo中获取20个文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61503326/

相关文章:

mongodb - 导出集合并用另一个集合中的字段替换字段(聚合?)

mongodb - 在mongoDB中通过_id搜索效率更高吗?

go - 如何在 golang 中打印到 stdout 的同时将 stdout 保存到字符串?

testing - 只在测试文件中引用的go测试代码会被编译成二进制文件吗?

go - 禁用第 3 方库的日志记录

MongoDB 在聚合期间替换值

mongodb - 如何在 MongoDB 中填充嵌套的引用数组?

linux - 执行并发 os/exec.Command.Wait() 时发生内存泄漏

go - go中的字符串和整数列表?

node.js - 将默认值设置为属性的新列, Mongoose 查询