MongoDB查询嵌套对象

时间:2020-06-12 19:25:49

标签: python mongodb discord pymongo

我的不和谐机器人有一个公会集合,看起来像这样:

{
  "_id": exampleId1,
  "prefix": [":dev: ", "!"],
  "muterole_id": null,
  "modrole_id": null,
  "modmail_channel_id": null,
  "modlog_channel_id": null,
  "log_channel_id": null,
  "verify_role_id": null,
  "verify_log_channel_id": null,
  "warn_mute_limit": null,
  "warn_kick_limit": null,
  "warn_ban_limit": null,
  "msg_xp": 5,
  "members": [
    {
      "_id": exampleId2,
      "roles": [],
      "quotes": [],
      "warns": 0,
      "xp": 0
    },
    {
      "_id": exampleId3,
      "roles": [],
      "quotes": [],
      "warns": 0,
      "xp": 0
    }
   ]

我正在尝试查询特定成员。因此,我只想返回一个成员,该成员的ID作为参数传入。但是此查询只是获取满足查询条件的文档的member字段。如何从对象数组中返回单个成员?

def get_member(self, member: discord.Member) -> dict:
        return self.guilds.find_one(
            {
                '_id': member.guild.id,
                'members._id': member.id
            },
            {
                'members': 1
            }
        )

0 个答案:

没有答案
相关问题