在PyMongo中的Mongo查询的输出中是否可以重命名字段? [英] Is it possible rename fields in the outputs of a Mongo query in PyMongo?
本文介绍了在PyMongo中的Mongo查询的输出中是否可以重命名字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Mongo中有一些文档:
I have some documents in 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}
是否可以在PyMongo的查找查询中将年龄"字段重命名为值"?
Is it possible to rename the 'age' field to 'value' in a find query in PyMongo?
推荐答案
我将aggregate
方法与$project
运算符一起使用.
I'd use the aggregate
method with $project
operator.
来自mongodb Web文档.
From mongodb web docs.
您也可以使用$ project重命名字段.考虑以下 例如:
You may also use $project to rename fields. Consider the following example:
db.article.aggregate(
{ $project : {
title : 1 ,
page_views : "$pageViews" ,
bar : "$other.foo"
}} );`
例如
db.mycol.aggregate({ $project : { name:1, value:"$age" }});
请参见 http://docs.mongodb.org/manual/reference/aggregation/#_S_project
这篇关于在PyMongo中的Mongo查询的输出中是否可以重命名字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文