mongodb - Join Through Map reduce -
i have 1 collection in student_id primary key:
test1:{student_id:"xxxxx"},
i have collection in student_id inside array of collection:
class:{"class":"i",students:["student_id":"xxxx"]}
my problem want join these 2 tables on basis of student id,
i using map reduce , out "merge", won't work.
my mr query follows.
db.runcommand({ mapreduce: "test1", map : function map() { emit(this._id,this); }, reduce : function reduce(key, values) { return values; }, out : { merge: "testmerge" } }); db.runcommand({ mapreduce: "class", map : function map() { emit(this._id,this); }, reduce : function reduce(key, values) { return values; }, out : { merge: "testmerge" } });
but inserts 2 rows.
can 1 guide me regarding this,i new mr
as in example want details of student "test1" collection,studying in class "i".
your requirement seems be:
as in example want details of student "test1" collection,studying in class "i".
in order that, store classes student in student:
{ student_id: "xxxxx", classes: ["i"], },
then can ask students information with:
db.students.find( { classes: "i" } );
without need slow , complex map reduce jobs. in general, should avoid map/reduce can't make use of indexes , can not run concurrently. need understand in mongodb operations done on one collection. there no such thing join, , trying emulate map/reduce bad idea. @ least can 2 queries:
// find students in class "i": ids = []; db.classes.find( { class: "i" } ).foreach(function(e) { ids.push( e.student_id ) ; } ); // result, find of students information: db.students.find( { student_id: { $in: ids } } );
but recommend redesign schema , store classes each student. general hint, in mongodb store relation between documents on other side compared relational database.
Comments
Post a Comment