我很难使用Pymongo查询Follwing嵌套的文档

发布于 2025-01-26 03:18:58 字数 1046 浏览 5 评论 0原文

如果这些是以下嵌套文档,

[
  {
    "_id": 5,
    "name": "Wilburn Spiess",
    "scores": [
      {
        "score": 44.87186330181261,
        "type": "exam"
      },
      {
        "score": 25.72395114668016,
        "type": "quiz"
      },
      {
        "score": 63.42288310628662,
        "type": "homework"
      }
    ]
  },
  {
    "_id": 6,
    "name": "Jenette Flanders",
    "scores": [
      {
        "score": 37.32285459166097,
        "type": "exam"
      },
      {
        "score": 28.32634976913737,
        "type": "quiz"
      },
      {
        "score": 81.57115318686338,
        "type": "homework"
      }
    ]
  },
  {
    "_id": 7,
    "name": "Salena Olmos",
    "scores": [
      {
        "score": 90.37826509157176,
        "type": "exam"
      },
      {
        "score": 42.48780666956811,
        "type": "quiz"
      },
      {
        "score": 96.52986171633331,
        "type": "homework"
      }
    ]
  }
]

我需要访问分数零件“类型” =考试。

有人可以帮我吗?

If these are the following nested documents

[
  {
    "_id": 5,
    "name": "Wilburn Spiess",
    "scores": [
      {
        "score": 44.87186330181261,
        "type": "exam"
      },
      {
        "score": 25.72395114668016,
        "type": "quiz"
      },
      {
        "score": 63.42288310628662,
        "type": "homework"
      }
    ]
  },
  {
    "_id": 6,
    "name": "Jenette Flanders",
    "scores": [
      {
        "score": 37.32285459166097,
        "type": "exam"
      },
      {
        "score": 28.32634976913737,
        "type": "quiz"
      },
      {
        "score": 81.57115318686338,
        "type": "homework"
      }
    ]
  },
  {
    "_id": 7,
    "name": "Salena Olmos",
    "scores": [
      {
        "score": 90.37826509157176,
        "type": "exam"
      },
      {
        "score": 42.48780666956811,
        "type": "quiz"
      },
      {
        "score": 96.52986171633331,
        "type": "homework"
      }
    ]
  }
]

I need to access the score part 'type' = exam.

Can somebody help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2025-02-02 03:18:58

如果您要求使用Python程序来访问分数,则可以像以下分数一样将它们打印出来:

collection = mongo_connection['db']['collection']
documents = collection.find({})
for doc in documents:
    for score in doc['scores']:
        if score['type'] == 'exam':
            print(f'Score: {score["score"]}')

如果您仅试图检索分数并忽略其余部分,我会在分数,$ match在类型上,然后投射所需的字段(或不)。

db.test.aggregate([
    {
        $unwind: '$scores'
    },
    {
        $match: {
            'scores.type': 'exam'
        }
    },
    {
        $project: {
            'name': '$name',
            'score': '$scores.score'
        }
    }
])

这将输出:

{
    "_id" : 5,
    "name" : "Wilburn Spiess",
    "score" : 44.8718633018126
},
{
    "_id" : 6,
    "name" : "Jenette Flanders",
    "score" : 37.322854591661
},
{
    "_id" : 7,
    "name" : "Salena Olmos",
    "score" : 90.3782650915718
}

If you're asking for a python program to access the score, you can print them out like:

collection = mongo_connection['db']['collection']
documents = collection.find({})
for doc in documents:
    for score in doc['scores']:
        if score['type'] == 'exam':
            print(f'Score: {score["score"]}')

If you are trying to retrieve only the scores and ignore the rest, I'd do an $unwind on the scores, $match on the type, and then project the fields you want (or not).

db.test.aggregate([
    {
        $unwind: '$scores'
    },
    {
        $match: {
            'scores.type': 'exam'
        }
    },
    {
        $project: {
            'name': '$name',
            'score': '$scores.score'
        }
    }
])

This would output:

{
    "_id" : 5,
    "name" : "Wilburn Spiess",
    "score" : 44.8718633018126
},
{
    "_id" : 6,
    "name" : "Jenette Flanders",
    "score" : 37.322854591661
},
{
    "_id" : 7,
    "name" : "Salena Olmos",
    "score" : 90.3782650915718
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文