MongoDB regex replace - javascript

My table name is : table_video
My field name in db is : url_value
Whose value is : http://192.168.1.124/test/abcd/abcd.m3u8
Value which is needed by me : http://192.168.1.124/test/abcd_NEW/abcd_NEW.m3u8
There are multiple values in place of "abcd", above url is just one example.
var cursor = db.table_video.find();
while (cursor.hasNext()) {
var x = cursor.next();
print("\n\n-----------------------------------");
print("Before : url_value : "+x['url_value']);
x['url_value'] = x['url_value'].replace(/^(.*?)\/test\/(.*?)\/(.*?)\.m3u8$/g, $1/test/$2_NEW/$2_NEW.m3u8);
print("After : url_value : "+x['url_value']);
db.table_video.update({_id : x._id}, x);
}
When I execute above command in mongo console, it gives an error : 2015-11-28T12:40:08.342+0530 ReferenceError: $1 is not defined
Any help is greatly appreciated

Your regex is wrong you can get the expected result using this regex1.
But the best way to do this is using "bulk" operations for maximum efficiency.
var bulk = db.table_video.initializeUnorderedBulkOp();
var count = 0;
db.table_video.find().forEach(function(doc) {
var newUrlValue = doc.url_value.replace(/^(.*?)\/test\/(.*?)\/(.*?)\.m3u8$/, '$1/test/$2_NEW/$3_NEW.m3u8');
bulk.find( { '_id': doc._id } ).updateOne( {
'$set': { 'url_value': newUrlValue }
});
count++;
if (count % 100 === 0) {
// Execute per 100 operation and re-init
bulk.execute();
bulk = db.table_video.initializeUnorderedBulkOp();
count = 0;
}
})
// Clean up queues
if (count > 0) bulk.execute();
If your MongoDB version is older that 2.6 you need to use a while loop.
var cursor = db.table_video.find();
while (cursor.hasNext()) {
var x = cursor.next();
print("\n\n-----------------------------------");
print("Before : url_value : "+x['url_value']);
var newUrlValue = x['url_value'].replace(/^(.*?)\/test\/(.*?)\/(.*?)\.m3u8$/, '$1/test/$2_NEW/$2_NEW.m3u8');
print("After : url_value : "+newUrlValue);
db.table_video.update({ _id : x._id }, { '$set': { 'url_value': newUrlValue } } );
}
Regex provided by #Uchiha.

in mongodb version 4.2 you have regexFind project operator which can be used to get the matches, then you ocan use substr to replace parts of the pattern

Related

firebase db splice like operation

if I have data in my db similar to this
/values:
value1: somevalue1
value2: somevalue2
specialValue1 : 1
specialValue2 : 2 //to be "removed" and rest values moved
specialValue3 : 3
specialValue4 : 4
how can I do "array splice" like operation, to get rid of specialValue2 and move the rest, so I get:
value1: somevalue1
value2: somevalue2
specialValue1 : 1
specialValue2 : 3
specialValue3 : 4
There is no splice-like operation in Firebase Realtime Database. You will have to read all the data, modify it in your application code, and write it back to the database.
In pure JavaScript the object manipulation would look like this:
var input = {
specialValue1 : 1,
specialValue2 : 2, //to be "removed" and rest values moved
specialValue3 : 3,
specialValue4 : 4
};
var keys = Object.keys(input);
var offset = 0;
var output = {};
keys.forEach((key, index) => {
if (input[key] != 2) {
output[keys[index-offset]] = input[key];
}
else {
offset = offset + 1;
}
});
console.log(output);

How to change the data type of embedded object - mongoDB?

I am having following document in the mongoDB. I want to change the currentVisit data type to integer
{
"_id" : ObjectId("5385e14f5caf98cc0712931c"),
"location" : {
"language" : null,
"country" : "null",
},
"request" : [
{
"currentVisit" : "1401292066",
"lastVisit" : "1401292066",
"visitedTime" : "1401282894"
}
]
}
Note
I had tried the following but it is not working.
db.visits.find().forEach( function (x) { x.request.currentVisit = parseInt(x.request.currentVisit); db.visits.save(x);});
Any suggestion will be grateful
You were close but you need to loop the inner array as well
db.visits.find().forEach(function (x) {
for ( var i=0; i <= x.request.length; i++ ) {
x.request[i].currentVisit = parseInt(x.request[i].currentVisit);
}
db.visits.save(x);
});
You probably want all of them really:
db.visits.find().forEach(function (x) {
for ( var i=0; i <= x.request.length; i++ ) {
x.request[i].currentVisit = parseInt(x.request[i].currentVisit);
x.request[i].lastVisit = parseInt(x.request[i].lastVisit);
x.request[i].visitedTime = parseInt(x.request[i].visitedTime);
}
db.visits.save(x);
});
Your request field is an array, so you should iterate over it to change the value "currentVisit" field in each subdocument:
db.visits.find().forEach( function (doc) {
doc.request.forEach(function(subdoc) {
subdoc.currentVisit = parseInt(subdoc.currentVisit);
});
db.visits.save(doc);
});

mongodb mapreduce does incorrect reducing

I'm running into some trouble with a very simple mapreduce, I can't figure out what I've done wrong. I'm trying to merge two collections together, and this first, db.Pos looks like this
"chr" : "chr1", "begin" : 39401, "end" : 39442
The other collection, db.Gene has the following format
"chr" : "chr1", "begin" : 39401, "end" : 39442, "gene" : "GENE1"
My code looks like this:
var mapPos = function(){
emit({chr: this.chr, begin:this.begin, end:this.end},{gene:""});
}
var mapGene = function() {
emit({chr: this.chr, begin:this.begin, end:this.end},{gene:this.gene});
}
r = function(key,values){
var result = {gene:""}
values.forEach(function(value){
result.gene = value.gene;
});
return result;
}
res = db.Pos.mapReduce(mapPos, r, {out: {reduce: 'joined'}});
res = db.Gene.mapReduce(mapGene, r, {out: {reduce: 'joined'}});
So what I'd like to see is a collection where entries that are matching by chr, begin, and end are merged and the gene field is filled in from the db.Gene collection.
Instead, I'm getting the "gene" field in my "joined" collection updated to something other than 0 even when there is no matching doc in db.Gene that has a gene field.
What did I do wrong?
After reflexion, i think you should use merge and not reduce for your out.
The reason why you don't have the good value :
The problem is when the reduce is applied between the joined collection content and the result of the db.Gene.mapReduce.
The function reduce don't know which value is the newest, so the result.gene returned is the last value.gene of the values array.
To distinguish the value that will override the value existing in the collection, you can add a flag.
res = db.Pos.mapReduce(
function() {
emit({chr: this.chr, begin:this.begin, end:this.end},{gene:this.gene || ''});
},
function(key,values){
var result = {};
values.forEach(function(value){
if (value)
result.gene = value.gene;
});
},
{out: {reduce: 'joined'}}
);
res = db.Gene.mapReduce(
function() {
//Add a flag override here
emit({chr: this.chr, begin:this.begin, end:this.end},{gene:this.gene || '', override: true});
},
function(key,values){
var result = {};
values.forEach(function(value){
if (value.override)
result.gene = value.gene;
});
return result;
},
{out: {reduce: 'joined'}}
);
Hope it's clear :)

Javascript Object Sort

My data :
books = {};
books[unique_id] = {
name : "Zimané Kurdi",
author : "Leyla Z.",
date : "2013.12.12",
y_no : 5
}
Output :
$.each(books, function(key, value){
// shorting "y_no"
});
How can I do? (shorting "y_no")
Thanks.
Assuming you're asking how you can sort, I wouldn't recommend using Object.each. Instead, set yourself up with an array, and sort that:
books = [];
books[unique_id] = { // assuming unique_id is numeric, otherwise it won't be an array member
name : "Zimané Kurdi",
author : "Leyla Z.",
date : "2013.12.12",
y_no : 5
};
// ... presumably adding more objects to books
function compare(a, b) {
if (a.y_no < b.y_no) {
return -1;
} else if (a.y_no > b.y_no) {
return 1;
}
return 0;
}
books.sort(compare);

Running out of memory using Node.js to optimise route pairings with mongodb database

Ive been trying to crack this for days, any help would be GREATLY appreciated. Basically im trying to iterate through this array of destination pairings and see if the overall distance can be improved by recalculating the mileage on each "inverted" pair. If the total mileage for the inverted pair is better than the original, I replace the original pairs with their inverted versions.
The Problem im finding is that I either run out of memory is I leave this process running, or the results aren't optimized in the way I expect ( i gain mileage).
I appreciate, I'm being a little sketchy here but im not sure where im going wrong. To put it into a constructive question: Can anyone help create a function/algorithm in JavaScript that can iterate through an array of pairings and optimise the same? ...please?
var firstResult = [{"from":"DE55 7RA","to":"DE55 4XJ","oNum":"3740003654","dNum":"3650645273","metres":"536"},{"from":"WF2 0XF","to":"WF2 0XE","oNum":"3740003632","dNum":"3650645256","metres":"304"},{"from":"WF9 2RH","to":"WF9 3AP","oNum":"3740003657","dNum":"3650645279","metres":"4132"},{"from":"LS10 1BG","to":"LS9 8HY","oNum":"3740003637","dNum":"3650645266","metres":"2941"},{"from":"WF10 5UA","to":"WF8 4QD","oNum":"3740003646","dNum":13,"metres":"7248"},{"from":"HD2 1UA","to":"WF15 6JL","oNum":"3740003641","dNum":"3650645270","metres":"6745"},{"from":"HU14 3HJ","to":"HU15 2PE","oNum":"3740003640","dNum":"3650645259","metres":"13309"},{"from":"WF2 0XF","to":"WF5 8NE","oNum":"3740003631","dNum":"3650645262","metres":"9862"},{"from":"WF9 2XX","to":"WF8 4QD","oNum":"3740003656","dNum":83,"metres":"13705"},{"from":"WF9 2XX","to":"WF8 4QD","oNum":"3740003647","dNum":67,"metres":"13705"},{"from":"WF9 2XR","to":"WF8 4QD","oNum":"3740003636","dNum":59,"metres":"13545"},{"from":"DN14 6XL","to":"DN14 0SB","oNum":"3740003642","dNum":"3650645268","metres":"20763"},{"from":"NG16 4HY","to":"NG15 0DR","oNum":"3740003635","dNum":"3650645258","metres":"13165"},{"from":"DN6 7FB","to":"S73 0UN","oNum":"3740003639","dNum":"3650645255","metres":"18835"},{"from":"LS25 6JH","to":"WF6 1GX","oNum":"3740003652","dNum":"3650645253","metres":"22861"},{"from":"DN3 3FF","to":"DN9 1LB","oNum":"3740003660","dNum":"3650645264","metres":"21343"},{"from":"DN6 7FB","to":"S71 3HQ","oNum":"3740003638","dNum":"3650645278","metres":"19268"},{"from":"S43 4UX","to":"S6 1LY","oNum":"3740003633","dNum":"3650645276","metres":"30365"},{"from":"S43 4UX","to":"S6 1LY","oNum":"3740003651","dNum":"3650645275","metres":"30365"},{"from":"DN14 6XL","to":"HU3 4UW","oNum":"3740003667","dNum":"3650645277","metres":"41502"},{"from":"LS25 6JH","to":"LS7 1PZ","oNum":"3740003653","dNum":"3650645274","metres":"25194"},{"from":"LS25 6JH","to":"BD4 0SG","oNum":"3740003658","dNum":"3650645269","metres":"43788"},{"from":"DN14 6XL","to":"HU7 0XS","oNum":"3740003648","dNum":"3650645265","metres":"49066"},{"from":"NG90 1BS","to":"DE6 1EX","oNum":"3740003643","dNum":"3650645261","metres":"44283"},{"from":"S80 3YY","to":"HD6 1LJ","oNum":"3740003665","dNum":"3650645260","metres":"80026"},{"from":"S80 2RZ","to":"BD8 9HQ","oNum":"3740003634","dNum":"3650645263","metres":"95649"},{"from":"NG90 1BS","to":"HU7 0YW","oNum":"3740003650","dNum":"3650645272","metres":"156190"},{"from":"NG90 1BS","to":"HU12 8TB","oNum":"3740003644","dNum":"3650645267","metres":"160285"},{"from":"NG90 1BS","to":"YO17 6YA","oNum":"3740003645","dNum":"3650645257","metres":"172388"},{"from":"NG90 1BS","to":"YO17 9HG","oNum":"3740003649","dNum":"3650645271","metres":"176868"}],
fs = require('fs'),
_ = require('underscore'),
mongoose = require('mongoose'),
Schema = mongoose.Schema,
logPostals = fs.createWriteStream('postals.txt', {'flags' : 'w'}),
db = mongoose.connect('mongodb://localhost/legs');
var Leg = new Schema({
from: {
type: String,
validate: /^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/i,
trim: true
},
to: {
type: String,
validate: /^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/i,
trim: true
},
time: Number,
distance: Number
})
var legModel = mongoose.model('Leg', Leg);
var legIndex = 0, armIndex= 0 , inverted = 0, original = 0;
var goCompare = function(){
_.each(firstResult , function(leg){
_.each(firstResult , function(arm){
if(leg.from !== arm.from){
legModel.find({
from: leg.from,
to: arm.to
}, function(err, data) {
if (data.length > 0) {
legModel.find({
from: arm.from,
to : leg.to
},function(e,d){
inverted = data[0].distance + d[0].distance;
original = (leg.metres*1) + (arm.metres*1) ;
if( original > inverted ){
legIndex = firstResult.indexOf(leg);
armIndex = firstResult.indexOf(arm);
firstResult[legIndex] ={
from : leg.from,
to : arm.to,
metres : data[0].distance,
oNum : leg.oNum,
dNum : arm.dNum
};
firstResult[armIndex] = {
from : arm.from,
to : leg.to,
metres : d[0].distance,
oNum : arm.oNum,
dNum : leg.dNum
};
_.each(firstResult , function( item ){
logPostals.write(item.from +'\t'+ item.to +'\t'+item.metres +'\r\n')
})
goCompare();
}else{
}//end of if distance comparison
})//end of function callback from search
}//end of if data...
}//end of err...data
)//end of find
}
})
})
}//end of go compare###############
goCompare();

Categories