Mongo Db bulk update operation for data type conversion - mongodb

How to perform bulk operation for mongo db? I already have script to change data type but it is not considering huge volume.
Collection 'store' has column called 'priceList' which is Array having multiple fields one of which is 'value'. Right now it is integer and now I want to convert it to custom record object.
Current schema
store
- _id
- name [String]
- priceList [Array]
- amount [Record] //{"unscaled":<value>, "scaled", <value>}
- value [Integer]
Need to convert value to [Record] as mentioned in above format
For e.g:- value: 2 will become value: {"unscaled":2, "scaled", 0};
db.store.find({priceList: { $exists : true}}).forEach(function(obj){
obj.priceList.forEach(function(y){
y.value = ({"unscaled":NumberLong(y.value),"scaled",NumberInt(0)});
db.store.save(obj);
})
});
Thanks!!

you try like this,
db.store.find({
priceList: {
$exists: true
}
}).forEach(function(myDoc) {
var child = myDoc.priceList;
for (var i = 0; i < child.length; i++) {
var ob = child[i];
var obj = {"unscaled":NumberLong(ob.value),"scaled":NumberInt(0)};
if ('value' in ob) {
ob.value = obj;
child[i] = ob;
}
}
db.store.update({
_id: myDoc._id
}, {
$set: {
subMenu: child
}
});
});

Hope this helps (updated) !
db.store.find({priceList: {$exists: true}})
.toArray()
.forEach(o => db.store.update(
{_id: o._id},
{ $set: {
priceList: o.priceList.map(l => Object.assign(l, {
amount: {
unscaled: l.value,
scaled: 0
}
}))
}
}
))

Related

Not iterable when using find ObjectId

I'm trying to find a certain document in my mongodb then update the int value of it using find query, I'm using $in because I used an array to find each element inside it, but when I used ObjectId it gives me error:
bloodinventoryDocs is not iterable
Here is what I did
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId('5c014c999cc48c3b0057988b');
var newValue = 1;
var newBloodgroup = "A_positive";
var newGetbloodcomponent = "Whole Blood";
Bloodinventory.find({ blood_component : { $in : newGetbloodcomponent} , blood_group: { $in :newBloodgroup},chapter: { $in :id}}, function(err, bloodinventoryDocs) {
for(let bloodinventory of bloodinventoryDocs) {
bloodinventory.num_stock = bloodinventory.num_stock + newValue ;
bloodinventory.save(function(err) {
if (err) {
console.log(err);
} else {
console.log('success');
}
});
}
});
Just use chapter: { $in: [id] }

Is it possible to Rename the output key in Mongo's MapReduce Result?

I am trying to perform an Inline mapreduce operation using pyMongo.
The code looks like this:
import pymongo
from bson.code import Code
con = pymongo.MongoClient()
map_func = Code("""
function() {
var all = this.members.concat(this.admins)
var group_id = this._id
all.forEach(function(_id) {
emit(_id, [group_id])
})
}
""")
reduce_func = Code("""
function(key, values) {
var ob = {};
ob[key] = [];
for (var i=0; i<values.length; i++) {
ob[key].push(values[i][0])
}
return ob
}
""")
finalize_func = Code("""
function(key, value) {
if (typeof(value.push) == "function") {
return value
} else {
return value[key]
}
}
""")
result = con.test.group.inline_map_reduce(
map_func,
reduce_func,
finalize=finalize_func)
import pprint; pprint.pprint(result)
Output for this operation is:
[{u'_id': u'135348133252952338363702',
u'value': [u'135457069105859781018098',
u'135661481520484615218098',
u'135391961249458761918098',
u'135758863859275369318098',
u'135156779012512657918098',
u'135285081801846289218098',
u'136040996346306049718098',
u'136237587535011048218098',
u'136862399436556328318098']},
{u'_id': u'136068596781820946163702',
u'value': [u'136068597966313224518098',
u'135156779012512657918098',
u'136415311739865096818098']}]
Is there any hook/operator by which I can rename the output field to any custom string example "group_id" instead of "values" ?
I have read Mongo's MapReduce documentation, but did not find any tip on how to accomplish this.
Using the MapReduce finalize hook described here, I reformatted my output to the field names I wanted:
db.collection.mapReduce(
mapFunction(),
reduceFunction(),
{
query: {params},
finalize: function(key, reduced_value) {
return {
my_field: key,
my_value: reduced_value
}
}
}
)
No, the reduced values are always in a field named value.

in mongo need to join two collections using Identity columns and emit the needed columns from both collections

I have book and author collection.in this name and works_written are the same value column respectively.so i tried the following script but it emit only first map values,second map values not emitted.
book = function() {
emit(this.id, {name: this.name,editions:this.editions});
}
author = function() {
emit(this.id, {name:this.name,works_written: this.works_writtten,});
}
r_b = function(k, values) {
var result = {};
values.forEach(function(value) {
var name;
for (name in value) {
if (value.hasOwnProperty(name)) {
result[name] = value[name];
}
}
});
return result;
};
r_a = function(k, values) {
var result = {};
values.forEach(function(value) {
var works_written;
for (works_written in value) {
if (value.hasOwnProperty(works_written)) {
result[works_written] = value[works_written];
}
}
});
return result;
};
res = db.book.mapReduce(book, r_ja, {out: {reduce: 'joined'}})
res = db.author.mapReduce(author, r_jp, {out: {reduce: 'joined'}})
can someone help me out?
From looking at your code, it seems like you have two collections, "book" and "author". Each book is structured as
{
id: <some id>,
name: <some name>,
editions: <comma-separated string of editions>
}
and each author is structured as
{
id: <some id>,
name: <some name>,
works_written: <comma-separated string of works written>
}
It would be more reasonable to store both works_written and editions as arrays rather than comma-separated lists each packed into an individual string. This would make iterating over the array possible.
Additionally, do you have multiple documents for each author and each book? If not, you do not need a mapreduce to do what you are attempting to do - a simple find() should work.
In case I have misinterpreted, what exactly are you attempting to do?

MongoDB/Mongoose: Can't put simplest MapReduce to work

Hello all I'm trying to do is to get the count of each distinct departmentType:
fnMap = function() {
emit(this.departments.departmentType, {typeCount:1} );
}
fnReduce = function(key, values) {
var result = {typeCount: 0};
values.forEach(function(value) {
result.typeCount += value.brandCount;
});
return result;
};
var command = {
mapreduce : "clients",
query : {"departments.departmentType": {$exists: true}},
map : fnMap.toString(),
reduce : fnReduce.toString(),
//sort: {"departments.departmentType":1},
out: {inline: 1}
};
mongoose.connection.db.executeDbCommand(command, function(err, dbres) {
});
When executing the command, dbres.documents[0].results only contains 1 item with the total number of departmentTypes, instead of several items one for each departmentType with its count.
Any ideas what am I doing wrong?
Also, when I uncomment the SORT line, I get error "db assertion failure: could not create cursor over...", I believe the field name is written correctly.
Mongoose v3 has now a Model.mapreduce() function (see doc).
The full example shown is:
var o = {};
o.map = function () { emit(this.name, 1) }
o.reduce = function (k, vals) { return vals.length }
o.out = { replace: 'createdCollectionNameForResults' }
o.verbose = true;
User.mapReduce(o, function (err, model, stats) {
console.log('map reduce took %d ms', stats.processtime)
model.find().where('value').gt(10).exec(function (err, docs) {
console.log(docs);
});
})
The problem with count i believe is because in your fnReduce() function you are summit the results instead of displaying them in an array.
You can use:
db.clients.distinct("departments.departmentType")
That will give an array with all the distinct departmentType values.
There were two problems in your map/reduce. One is brandCount in reduce rather than typeCount. But more importantly, you are trying to emit once per document, when you need to emit once per department array element. Corrected (and slightly simplified) code:
> fnMap = function () {
this.departments.forEach(
function (d) {
emit(d.departmentType, 1);
}
);
}
> fnReduce = function (key, values) {
var result = 0;
values.forEach(
function (value) {result += value;});
return result;
}

When parsing a MongoDB object (using Mongoose), how come I can't get item elements?

IndexedTweets.find(searchParameters, function(err, indexedTweetsResults) {
var chunkSize, count, resultArray, size;
if (err != null) {
return console.log("Error!");
} else {
size = indexedTweetsResults.length;
count = 0;
chunkSize = 100;
resultArray = [];
indexedTweetsResults.forEach(function(tweet) {
console.log(tweet.user);
});
}
});
That's my code. My result looks like:
{ text: 'stuff',
user:
{ display_name: '...',
screen_name: '...'},
}
So why can't I get tweet.user? It just returns undefined.
If you are just getting back a string of JSON from mongo, you'll need to call JSON.parse(). If that's not the case then you should provide more code because it's not clear what the issue is.