简体   繁体   English

如何在pymongo中将特定属性的所有值设置为小写

[英]How to set all values of a specific attribute to lowercase in pymongo

I am using pymongo to update attributes in a mongodb database. 我正在使用pymongo更新mongodb数据库中的属性。 I have an attribute gender, which contains, for example, male MALE and Male, and i need those to match. 我有一个性别属性,例如,其中包含male MALE和Male,我需要匹配这些属性。 I have tried: 我努力了:

mongo_collection.aggregate([{ $project: { Gender:{ $toLower:"$Gender"}, src:1}}])

but it said the sytax was invalid. 但它说该语法无效。

Let me know if you have any insight on why the syntax is invalid? 让我知道您对语法无效的原因有何见解?

I am using pymongo 3.6 and have been doing such queries there. 我正在使用pymongo 3.6,并在那里进行了此类查询。 I think the first mistake is you haven't enclosed '$toLower' in quotes ''. 我认为第一个错误是您没有在引号中加上'$ toLower'。 If you haven't defined Gender variable somewhere you might need to enclose that as well in quotes. 如果您尚未在某处定义Gender变量,则可能还需要将其用引号引起来。 For me the working syntax is below - 对我来说,工作语法如下:

mycoll.aggregate([{'$project':{'itemName':{'$toUpper':'$item'},'itemNo':1}}])

If i leave $toUpper without quotes , it tells me invalid syntax at $ and i think its same what you are encountering. 如果我将$ toUpper保留为不带引号,它会告诉我$处的语法无效,并且我认为与您遇到的情况相同。

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

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