mongodb - 在 MongoDB 中查找所有并修改文档
问题描述
我在 MongoDB 中有这样的文档
{"_id":{"$oid":"5eb8c55230cb8651e0906d7e"},"rolecode":"DHBK_ROLE_01","functioncode":"DHBK_FUNC_01, DHBK_FUNC_02","productid":"mBaaS_Platform","comid":"DHBK"}
{"_id":{"$oid":"5eb8c5a030cb8651e0906d7f"},"rolecode":"DHBK_ROLE_02","functioncode":"DHBK_FUNC_02, DHBK_FUNC_03","productid":"GIS_Platform","comid":"DHBK"}
{"_id":{"$oid":"5ebe0016b4146803d8ad2559"},"rolecode":"DHKT_ROLE_01","functioncode":"DHKT_FUNC_01, DHKT_FUNC_02","productid":"mBaaS_Platform","comid":"DHKT"}
{"_id":{"$oid":"5ebe003cb4146803d8ad255a"},"rolecode":"DHKT_ROLE_02","functioncode":"DHKT_FUNC_01","productid":"Analysis_Platform","comid":"DHKT"}
{"_id":{"$oid":"5ebe00ffb4146803d8ad255b"},"rolecode":"DHBK_ROLE_03","functioncode":"DHBK_FUNC_01, DHBK_FUNC_02,DHBK_FUNC_03","productid":"IOT_Platform","comid":"DHBK"}
{"_id":{"$oid":"5ecf89f969f07e1cb0ad063d"},"rolecode":"DHBK1_ROLE_01","functioncode":"DHBK1_FUNC_1,DHBK_FUNC_03","productid":"IOT_Platform","comid":"DHBK1"}
{"_id":{"$oid":"5ecf8b6169f07e1cb0ad063e"},"rolecode":"DHBK1_ROLE_02","functioncode":"DHBK1_FUNC_1,DHBK_FUNC_02","productid":"SSO_Platform","comid":"DHBK1"}
{"_id":{"$oid":"5ecf8b7969f07e1cb0ad063f"},"rolecode":"DHBK1_ROLE_03","functioncode":"DHBK1_FUNC_1","productid":"ABC_Platform","comid":"DHBK1"}
我想找到文档DHBK1_FUNC_1
并functioncode
修改此属性为""
例如:
第 1 步:找到文件有DHBK1_FUNC_1
,functioncode
我们有:
{
"rolecode": "DHBK1_ROLE_01",
"functioncode": "DHBK1_FUNC_1,DHBK_FUNC_03",
"productid": "IOT_Platform",
"comid": "DHBK1"
}
{
"rolecode": "DHBK1_ROLE_02",
"functioncode": "DHBK1_FUNC_1,DHBK_FUNC_02",
"productid": "SSO_Platform",
"comid": "DHBK1"
}
{
"rolecode": "DHBK1_ROLE_03",
"functioncode": "DHBK1_FUNC_1",
"productid": "ABC_Platform",
"comid": "DHBK1"
}
第2步:修改,我们有:
{
"rolecode": "DHBK1_ROLE_01",
"functioncode": ",DHBK_FUNC_03",
"productid": "IOT_Platform",
"comid": "DHBK1"
}
{
"rolecode": "DHBK1_ROLE_02",
"functioncode": ",DHBK_FUNC_02",
"productid": "SSO_Platform",
"comid": "DHBK1"
}
{
"rolecode": "DHBK1_ROLE_03",
"functioncode": "",
"productid": "ABC_Platform",
"comid": "DHBK1"
}
先感谢您
解决方案
句法:
db.collection.updateMany({filter},{update})
解决方案:
Replace collection_name with your collection.
db.collection_name.updateMany({"functioncode": "DHBK1_FUNC_1",},{'$set':{"functioncode":""},})
推荐阅读
- html - 如何对齐两个 div - 一个居中,另一个居中 - 两个 div 都可变宽度
- drupal - Drupal 的仪表板未正确显示
- navigation - Liferay.SPA.app.navigate 不会在没有路径的情况下重定向
- swiftui - 有没有办法在 SwiftUI 中“激活”自动换行?
- php - 我们如何在 Laravel 的单个控制器中处理 50 个依赖注入?
- ruby-on-rails - Rails 应用程序如何在默认情况下单独使用 Puma 运行 - 没有 Web 服务器
- apache-kafka - 创建刷新流时的问题
- c++ - 为 Windows 构建 SpiderMonkey
- firebase - 你还能在新项目中使用 XMPP 和 Firebase 云消息传递吗?
- javascript - 我的不和谐机器人无法正确发送对命令的多个响应