简体   繁体   English

是否可以在PyMongo中重命名Mongo查询的输出中的字段?

[英]Is it possible rename fields in the outputs of a Mongo query in PyMongo?

I have some documents in Mongo: 我在Mongo有一些文件:

{"name" : "John", "age" : 26}
{"name" : "Paul", "age" : 34}
{"name" : "George", "age" : 36}

and another function that expects documents of the form: 和另一个需要表单文档的函数:

{"name" : "XXX", "value" : YY}

Is it possible to rename the 'age' field to 'value' in a find query in PyMongo? 是否可以在PyMongo中的查询查询中将'age'字段重命名为'value'?

I'd use the aggregate method with $project operator. 我将使用$project运算符的aggregate方法。

From mongodb web docs. 来自mongodb web docs。

You may also use $project to rename fields. 您也可以使用$ project重命名字段。 Consider the following example: 请考虑以下示例:

db.article.aggregate(
 { $project : {
     title : 1 ,
     page_views : "$pageViews" ,
     bar : "$other.foo"
 }} );`

eg 例如

db.mycol.aggregate({ $project : { name:1, value:"$age" }});

see http://docs.mongodb.org/manual/reference/aggregation/#_S_project http://docs.mongodb.org/manual/reference/aggregation/#_S_project

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM