当前位置:网站首页>Mongodb replaces some contents of a field
Mongodb replaces some contents of a field
2022-07-21 23:12:00 【King, I'll patrol the mountain myself】
demand
mongoDB There is no definition replace Functional , that , If there is a need to replace nongo Part of the data in , What do I do ? because mongo Of shell Actually, it 's just js Script executor , therefore ,js Actually, it can be implemented , therefore , We can find out exactly what needs to be executed replace On the basis of , use js Realization replace.
The demand I met was , Replace all mongo in domain Field , With Caleb At the beginning , Replace all with go/trend.
Inquire about
First , You need to be able to query all domain Location of field , Because of my mongo The structure is more complicated , Arrays nested arrays , therefore , I need complex queries here . For a detailed explanation of complex query statements, please refer to my last blog :mongo Complex queries , Nested multi-level arrays and regular expressions
This is my query statement :
datasource Under the array stct,stct Below views Array ,views Array metricView,metricView Under the domain Field , Asked to caleb start , Use regular expressions to implement :
db.getCollection('richPage').find({
"dataSources":{
$elemMatch:{
"stct.views":{
$elemMatch:{
"metricView.domain":{
$regex:"Caleb",$options:"$i"}}}}}})
replace
First, we can accurately locate and query the fields that need to be changed , You can execute replace Operation , Script directly , Explain it slowly :
db.getCollection('richPage').find({
"dataSources":{
$elemMatch:{
"stct.views":{
$elemMatch:{
"metricView.domain":{
$regex:"Caleb",$options:"$i"}}}}}}).forEach(
function(item) {
var dataSource = item['dataSources'];
for(var i in dataSource){
var stct = dataSource[i]['stct'];
if(stct != null){
var views = stct['views'];
if(views != null){
for(var j in views){
var domain = views[j]['metricView']['domain'];
var domainUid = views[j]['metricView']['domainUid'];
if(domain != null && String(domain).search('Caleb') > -1){
domain = domain.replace('Caleb', 'Go/Trends');
domainUid = domainUid.replace('caleb', 'go/trends');
views[j]['metricView']['domain'] = domain;
views[j]['metricView']['domainUid'] = domainUid;
}
}
}
}
}db.getCollection('richPage').updateOne({
"_id":item['_id']},{
$set:{
"dataSources":dataSource,"exec":1}});
}
);
because mongo It's not replace Functional , Again because mongo Of shell The interpreter is actually a js Interpreter , therefore ,js It can be used , that :
.forEach: Is the result of the query , Traversal
item: Is to traverse every result set
item[‘dataSources’]: Take the data in brackets ( I need a special explanation here ,mongo Of shell The script version is different , There may also be differences in value taking methods , What I have here is js Methods in scripts , The pro test can successfully take values )
var domain: use var After receiving data , use js Medium replace Method replacement requires changes , Then reassign :views[j][‘metricView’][‘domain’] = domain
“_id”:item[‘_id’]: Is to select the one to update item
“dataSources”:dataSource": here dataSource stay js Has been reassigned , We need to replace The fields of are contained in dataSource in , So update this attribute , Of course, it can be something else , As long as it can include all your changes
边栏推荐
猜你喜欢
随机推荐
OSPF comprehensive experiment
Hcip day 4
Hcip day 1 operation
对这几年的编程旅路的小总结···
ensp静态路由实验
HCIP 第十天
Rip experiment
Unity customized gadget "image removal mipmap"
log4j的简单配置
HCIA static comprehensive experiment
Simple configuration of log4j
OSPF optimization
Why does okcc call center introduce voice into the web?
C implementation of left leaning reactor
函数的参数
Function and application of LAN telephone software system
Hcip day 7
NAT的动作原理
An easy-to-use unity touch Manager
“FileInputStream“和“BufferedInputStream“的区别