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

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?

Related

Mongo Db bulk update operation for data type conversion

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
}
}))
}
}
))

mongodb showing array of null when printing the outside of query

i am trying to push the resultant of the count to an array in mogodb query, while pushing it showing the array after that if print it outside of query it is showing empty array.
collection1 in db is like below
[{title:Home,
date:24-10-2016},
{title:Accesories,
date:13-02-2016}
]
my code
exports.listOfCategories=function(req,res){
collection1.find().exec(function (err, categories) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
var categoryList = categories;
var catTitle;
var allCat = [];
// console.log(categoryList);
for (var i = 0; i < categoryList.length; i++) {
catTitle = categoryList[i].title;
contentCounts(catTitle);
function contentCounts(content, callback) {
var catName = new RegExp(content, 'i');
var mongoQuery = {
"ProCategory.title": catName
}
collection2.find(mongoQuery).count(function (err, count) {
generateContentArr(content, count)
});
}
function generateContentArr(content, count) {
allCat.push({
name: content,
count: count
});
console.log(JSON.stringify(allCat));
// Here it is showing the array what i pushed
}
}
console.log(JSON.stringify(allCat));
// Here it not showing the total array, it showing an empty array
res.json(allCat);
}
});
}
Thanks in advance
You are not waiting for the result of an async operation, in your case in the for loop you need to wait for the result of mongo operation, but as for loop is synchronous, you are just making calls to mongo but don't wait for the results, and print the empty array right after the loop.
I would suggest you to use promises instead of callbacks, I don't know which version of mongoose you are using but the last version have promise support for mongo methods like find and count. Here is an example for your case:
var Promise = require("bluebird");
function countByTitle(catTitle){
var mongoQuery = {"ProCategory.title": new RegExp(catTitle, 'i')}
return collection2.count(mongoQuery).then(function(count) {
return {
name: catTitle,
count: count
};
});
}
collection1.find().then(function (categories) {
var categoryList = categories;
var promises = [];
for (var i = 0; i < categoryList.length; i++) {
promises.push(countByTitle(categoryList[i].title));
}
return Promise.all(promises).then(results => {
console.log(JSON.stringify(results));
})
}).catch(function (err) {
//if there is any error while resolving the promises, this block will be called
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
});

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.

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;
}

how to do distinct and group in mongodb?

how to do a mysql query
SELECT COUNT(DISTINCT ip), COUNT(DISTINCT area) FROM visit_logs GROUP BY t_hour
in mongodb without multi mapreduct?
You have to keep the list of "keys" in your objects, and compute your count as the count of the distinct keys; this can be done in the finalize method in MongoDb's map/reduce.
Something like (untested):
var mapFn = function() {
emit(this.t_hour, { ips: [this.ip], areas: [this.area] );
};
var reduceFn = function(key, values) {
var ret = { ips: {}, areas: {} };
// objects used as "sets"
var ips = {};
var areas = {};
values.forEach(function(value) {
value.ips.forEach(function(ip) {
if (!ips[ip]) {
ips[ip] = true; // mark as seen
ret.ips.push(ip);
}
});
value.areas.forEach(function(area) {
if (!areas[area]) {
areas[area] = true; // mark as seen
ret.areas.push(area);
}
});
});
};
var finalizeFn = function(key, value) {
return { ips: value.ips.length; areas: value.areas.length };
}