mongodb 批量转换大写字符

本文介绍了一种在MongoDB中批量将指定字段转换为大写的有效方法。通过JavaScript函数实现转换,并提供了成功的实现案例及两种失败尝试的原因解析。

需求

需要批量处理数据库中的某一字段为大写

语句

方式一 失败 应该也是函数不支持 toUpper只能聚合时使用

//批量处理兑换码大写   失败
db.gift_card.find({cdKey:{$exists:true}}).forEach( 
function(item){ 
	print({$toUpper:item.cdKey})
	db.gift_card.update(
	{_id:item._id},
	{
	$set:{"cdKey":{$toUpper:item.cdKey}} 
	}
	)
	}
);

方式二 失败

> db.gift_card.update({cdKey:{$exists:true},status:0},{$set:{"cdKey":{$toUpper:"$cdKey"}}},false,true)
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 0,
	"nModified" : 0,
	"writeError" : {
		"code" : 52,
		"errmsg" : "The dollar ($) prefixed field '$toUpper' in 'cdKey.$toUpper' is not valid for storage."
	}
})

原因是update中不支持$toUpper 该操作符是管道操作符
在这里插入图片描述
方案三 成功
通过js 函数转换大写在进行赋值 toLowerCase( ) .toUpperCase();

db.gift_card.find({cdKey:{$exists:true},status:0}).forEach( 
function(item){ 
	var upcaseName = item.cdKey;
	
	var upperCase = upcaseName.toUpperCase(); 
	
	print(upcaseName +" --大写-->  "+upperCase )
	db.gift_card.update(
	{_id:item._id},
	{
	$set:{"cdKey": upperCase} 
	}
	)
	}
);

参考链接

$toUpper (aggregation)

$exists
update

js String相关函数

虽然提供的引用中未直接提及C++ MFC项目中mongodb字符转换的方法,但可以结合通用的字符编码转换知识以及MFC和mongodb的特点来分析。 在C++ MFC项目中,常见的字符编码有ANSI、UNICODE等,而mongodb存储数据时可能涉及不同编码格式的转换。 对于字符编码转换,在VC++中可以利用Windows API函数进行ANSI、UNICODE与UTF - 8之间的转换。例如在MFC中,将UNICODE字符转换为UTF - 8字符串可以使用`WideCharToMultiByte`函数,示例代码如下: ```cpp #include <Windows.h> #include <string> std::string UnicodeToUTF8(const std::wstring& wstr) { int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL); if (len == 0) { return ""; } std::string str(len, 0); WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], len, NULL, NULL); return str; } ``` 将UTF - 8字符转换为UNICODE字符串可以使用`MultiByteToWideChar`函数,示例代码如下: ```cpp #include <Windows.h> #include <string> std::wstring UTF8ToUnicode(const std::string& str) { int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0); if (len == 0) { return L""; } std::wstring wstr(len, 0); MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], len); return wstr; } ``` 在与mongodb交互时,如果从MFC项目中获取的数据是ANSI或UNICODE编码,而mongodb存储需要UTF - 8编码,就可以使用上述函数进行转换。 当使用mongodb的C++驱动操作数据库时,将转换后的UTF - 8字符串插入到mongodb中,示例代码如下(需要包含mongodb的C++驱动头文件): ```cpp #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> #include <bsoncxx/builder/stream/document.hpp> #include <bsoncxx/types.hpp> void InsertDataToMongoDB(const std::string& utf8Str) { mongocxx::instance inst{}; mongocxx::client conn{mongocxx::uri{"mongodb://localhost:27017"}}; auto db = conn["testdb"]; auto collection = db["testcollection"]; bsoncxx::builder::stream::document document{}; document << "content" << utf8Str; collection.insert_one(document.view()); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值