You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PyMongo仅输出聚合查询结果中的书名字段内容

How to Extract Only the Book Name from PyMongo Aggregation Result

Hey there! No worries, this is super straightforward. Your books variable is a standard Python dictionary, so you just need to access the value tied to the _id key (which holds the book name you want).

Here's what you can do:

  • If you just need to print the name directly (including any trailing spaces from the original result):
    print(books['_id'])
    
  • Since your result shows 'Harry Potter ' with an extra trailing space, to get clean Harry Potter without unwanted whitespace, add the strip() method to trim any leading/trailing spaces:
    print(books['_id'].strip())
    

That's it! This will pull only the book name from your aggregation result and print it exactly as you requested.

内容的提问来源于stack exchange,提问作者Will.S89

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:05:26