Last active
August 29, 2015 14:02
-
-
Save harpreetkhalsagtbit/f4a7167555056ace5d08 to your computer and use it in GitHub Desktop.
Populate Multiple Tables - Result of each populate is an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// working example mongoose | |
var tableASchema = new Schema({ | |
template : String, | |
description : String | |
}); | |
var tableBSchema = new Schema({ | |
group : String, | |
a_pk : [{ type: Schema.ObjectId, ref: 'tableA' }], | |
}); | |
var tableCSchema = new Schema({ | |
project : String, | |
b_pk : [{ type: Schema.ObjectId, ref: 'tableB' }] | |
}) | |
var tableA = mongoose.model('tableA', tableASchema); | |
var tableB = mongoose.model('tableB', tableBSchema); | |
var tableC = mongoose.model('tableC', tableCSchema); | |
// var tableRel = mongoose.model('tableRel', tableRelSchema); | |
// var valB = new tableA({group: 'Octo', description: 'octo rading SA'}); | |
// var valA = new tableA({template:'BSS'}); | |
// var valB = new tableA({group: 'KBH', description: 'Octo Trading SA'}); | |
// var valA = new tableA({template:'POA'}); | |
// var valA = new tableA({template:'LOC'}); | |
// var valA = new tableA({template:'Litigation'}); | |
// valA.save(function(err, result) { | |
// console.log('A created', result) | |
// }) | |
// valB.save(function(err, result) { | |
// console.log('B created', result) | |
// }) | |
// Relationship Table Between - Group and Templates | |
// eg: Sample Entry 1, KBH contains LOC and Litigation | |
// | |
// tableA.find({ | |
// $or:[ | |
// { | |
// template:'LOC' | |
// }, | |
// { | |
// template:'Litigation' | |
// } | |
// ]}, function(err, resultA) { | |
// console.log(resultA) | |
// var obj = { | |
// group:"KBH", | |
// a_pk:[] | |
// } | |
// for(each in resultA) { | |
// obj.a_pk.push(resultA[each]._id) | |
// } | |
// var valB = new tableB(obj); | |
// valB.save(function(err,result) { | |
// if(err) { | |
// console.log(err) | |
// } else { | |
// console.log(result) | |
// } | |
// }) | |
// }); | |
// eg: Sample Entry 2, Octo contains BSS and POA | |
// | |
// tableA.find({ | |
// $or:[ | |
// { | |
// template:'BSS' | |
// }, | |
// { | |
// template:'POA' | |
// } | |
// ]}, function(err, resultA) { | |
// console.log(resultA) | |
// var obj = { | |
// group:"Octo", | |
// a_pk:[] | |
// } | |
// for(each in resultA) { | |
// obj.a_pk.push(resultA[each]._id) | |
// } | |
// var valB = new tableB(obj); | |
// valB.save(function(err,result) { | |
// if(err) { | |
// console.log(err) | |
// } else { | |
// console.log(result) | |
// } | |
// }) | |
// }); | |
// | |
// Relationship Table Between - Project and Groups | |
// eg: Sample Entry 1, Aster contains KBH(group) | |
// KBH contains Litigation and LOC | |
// | |
// tableB.find({ | |
// group:"KBH" | |
// }, function(err, resultB) { | |
// console.log(resultB) | |
// var obj = { | |
// project:"Aster", | |
// b_pk:[] | |
// } | |
// for(each in resultB) { | |
// obj.b_pk.push(resultB[each]._id) | |
// } | |
// var valC = new tableC(obj); | |
// valC.save(function(err,result) { | |
// if(err) { | |
// console.log(err) | |
// } else { | |
// console.log(result) | |
// } | |
// }) | |
// }); | |
// Relationship Table Between - Project and Groups | |
// eg: Sample Entry 1, Zesco contains Octo(group) | |
// Octo contains BSS and POA | |
// | |
// tableB.find({ | |
// group:"Octo" | |
// }, function(err, resultB) { | |
// console.log(resultB) | |
// var obj = { | |
// project:"Zesco", | |
// b_pk:[] | |
// } | |
// for(each in resultB) { | |
// obj.b_pk.push(resultB[each]._id) | |
// } | |
// var valC = new tableC(obj); | |
// valC.save(function(err,result) { | |
// if(err) { | |
// console.log(err) | |
// } else { | |
// console.log(result) | |
// } | |
// }) | |
// }); | |
// Now Populate Data | |
// eg: Sample Entry 1, Get all Groups and Templates that belongs | |
// to Aster | |
// | |
// tableC.findOne({ | |
// project:"Aster" | |
// }) | |
// .populate('b_pk', 'group') | |
// .exec(function (err, group) { | |
// if (err) throw err; | |
// console.log(group) | |
// async.forEach(group.b_pk, function(group) { | |
// console.log(group, 'group') | |
// tableB.find({ | |
// _id:group.id | |
// }) | |
// .populate('a_pk', 'template') | |
// .exec(function (err, template) { | |
// if (err) throw err; | |
// console.log("all folders =", template); | |
// }) | |
// }) | |
// }) | |
// Now Populate Data | |
// eg: Sample Entry 1, Get all Groups and Templates that belongs | |
// to Zesco | |
// | |
tableC.findOne({ | |
project:"Zesco" | |
}) | |
.populate('b_pk', 'group') | |
.exec(function (err, project) { | |
if (err) throw err; | |
console.log('project', project) | |
console.log('') | |
console.log('groups', project.b_pk) | |
console.log('') | |
console.log('') | |
console.log('search for templates') | |
async.forEach(project.b_pk, function(group) { | |
console.log(group, 'group') | |
tableB.find({ | |
_id:group.id | |
}) | |
.populate('a_pk', 'template') | |
.exec(function (err, template) { | |
if (err) throw err; | |
console.log("all folders =", template); | |
}) | |
}) | |
}) | |
// //output | |
// { | |
// project: 'Zesco', | |
// _id: 539f230025ce1a24074dd384, | |
// __v: 0, | |
// b_pk: [ { group: 'Octo', _id: 539f210dacc642b805ad5b1f } ] | |
// } | |
// { group: 'Octo', _id: 539f210dacc642b805ad5b1f } 'group' | |
// all folders = [ { group: 'Octo', | |
// _id: 539f210dacc642b805ad5b1f, | |
// __v: 0, | |
// a_pk: | |
// [ { template: 'BSS', _id: 539f1d895b32fe980a3928f3 }, | |
// { template: 'POA', _id: 539f1d93b0c784180b81c9d9 } | |
// ] | |
// }] |
Author
harpreetkhalsagtbit
commented
Jun 16, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment