Saturday, 15 January 2011

javascript - Mongojs fetching data from two collections and merging the result -



javascript - Mongojs fetching data from two collections and merging the result -

i mongodb newbie , appreciate help on problem described below.

i have 2 collections "users" , "bags". user collections scheme has {username, firstname, lastname} , handbag collection schema has {username, bagname, bagimage}.

while fetching users bags, want display firstname , lastname. problem cant seem able frame query correctly. using nodejs , mongojs driver. below query fetching bags

thmconfig.db.bags.find({status: "1"}).sort({$natural:-1}, function(err, data) { var baglist = '{"bags":['; if( err || !data) res.send('[{"status": "0"}]'); else data.foreach( function(innerdata) { console.log(innerdata.username); baglist += json.stringify(innerdata)+","; /* lke append firstname users collection */ }); console.log(baglist.slice(0,1)); res.write(maglist.slice(0,-1)); res.end(']}'); });

i appreciate help or pointers this. dont have selection changing driver, want implement using mongojs now.

thanks , regards, titash

i don't think have much selection other reading users collection , doing "join" operation programmatically. can either read user document per each handbag (inside loop), or read entire users collection object in advance, , lookups username

javascript node.js mongodb mongojs

No comments:

Post a Comment