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}
Is it possible to rename the 'age' field to 'value' in a find query in PyMongo?
The $rename operator updates the name of a field and has the following form: {$rename: { <field1>: <newName1>, <field2>: <newName2>, ... } } The new field name must differ from the existing field name. To specify a <field> in an embedded document, use dot notation.
The PyMongo function rename() is used to rename a collection. The rename operation fails if the new name is not an instance of basestring or it is an invalid collection name. Parameters : new_name : The new name of the collection.
The find_One() method of pymongo is used to retrieve a single document based on your query, in case of no matches this method returns nothing and if you doesn't use any query it returns the first document of the collection.
ASP.NET Core 3 MVC Application with MongoDByourCollectionName. insertOne({“YourFieldName”:yourValue, “yourFieldName”:”yourValue”,....... N}); If you want a single record from a collection, you can use findOne() and in order to get all records from the collection, you can use find().
I'd use the aggregate
method with $project
operator.
From mongodb web docs.
You may also use $project to rename fields. Consider the following example:
db.article.aggregate(
{ $project : {
title : 1 ,
page_views : "$pageViews" ,
bar : "$other.foo"
}} );`
e.g.
db.mycol.aggregate({ $project : { name:1, value:"$age" }});
see http://docs.mongodb.org/manual/reference/aggregation/#_S_project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With