i'm trying make query between 2 collections in mongodb using node js.
i have these 2 collection in db:
trip
{ "_id":objectid("55a922531e35772c1b17d4a0"), "name":"trip_name", "waypoints":[ "4c828999d8086dcb03877752", "4dd2ae657d8b4c6585f1a6fd", "4c59c3e4f346c928a8634dca" ], "line_points":[ [ 42.850937, 13.569256 ], [ 42.85109, 13.569377 ], [ 42.851131, 13.569225 ] ], "time":"00:10:23", "distance":6.622, "hashkey":"object:8207" }; poi
{ "_id":"4c828999d8086dcb03877752", "type":"feature", "geometry":{ "type":"point", "coordinates":[ 13.575249910354614, 42.85484995890166 ] }, "properties":{ "title":"lorenz cafè", "id":"4c828999d8086dcb03877752", "poi-type":1, "category":"ristorazione", "subcategory":"café", "categoryids":"4bf58dd8d48988d16d941735", "marker-color":"#ff7519", "marker-size":"small", "marker-symbol":"restaurant", "indirizzo":"piazza del popolo, 5", "citta":"ascoli piceno", "regione":"marche" } through route, query according id step , query restore json this:
result want
{"_id":"55a922531e35772c1b17d4a0","name":"trip_name","waypoints":[{"4c828999d8086dcb03877752":{"title":"lorenz cafè","id":"4c828999d8086dcb03877752","category":"ristorazione","position":{"lat":42.85484995890166,"lng":13.575249910354614}}},{"4dd2ae657d8b4c6585f1a6fd":{"title":"ottica di ferdinando","id":"4dd2ae657d8b4c6585f1a6fd","category":"negozi","position":{"lat":42.85485741498569,"lng":13.57675423240643}}},{"4c59c3e4f346c928a8634dca":{"title":"leopoldus ristorante","id":"4c59c3e4f346c928a8634dca","category":"ristorazione","position":{"lat":42.85648980743132,"lng":13.575512766838072}}}],"line_points":[[42.850937,13.569256],[42.85109,13.569377],[42.851131,13.569225]],"time":"00:10:23","distance":6.622} i implemented route :
/* trip id */ var j=0; router.get('/:id', function(req, res) { db = req.db; var idstring=req.params.id; var objid = new objectid(idstring); var collection2=db.get('poilist'); var collection = db.get('trip'); collection.find({"_id": objid},{},function(e,docs){ var trip=docs[0]; var poi=[]; var wp=trip.waypoints; for(var i=0; i< wp.length; i++) { collection2.find({"properties.id": wp[i]},function(e,docs2){ poi[j]={ "title" : docs2[0].properties.title, "id": docs2[0].properties.id, "category": docs2[0].properties.category, "position": {"lat":docs2[0].geometry.coordinates[1], "lng":docs2[0].geometry.coordinates[0]} }; var id = wp[j]; wp[j]= {}; wp[j][id]=poi[j]; if(j==wp.length-1){ console.log('emit '+j); emitter.emit('attesa' + j); }else{ j++; } }); } emitter.once('attesa'+ (trip.waypoints.length-1) ,function() { j=0; console.log('once'); res.json(trip); }); }); }); this route works if make multiple calls simultaneously, no longer work.
i have idea: count number of requests , go double check if id of place in trip seems expensive. know more practical method making queries multiple collections in mongo. in advance
first recommend use mongoose. before must prepare models, example:
var mongoose = require('mongoose') , schema = mongoose.schema var personschema = schema({ _id : number, name : string, age : number, stories : [{ type: schema.types.objectid, ref: 'story' }] }); var storyschema = schema({ _creator : { type: number, ref: 'person' }, title : string, fans : [{ type: number, ref: 'person' }] }); var story = mongoose.model('story', storyschema); var person = mongoose.model('person', personschema); and after when want "join" use next method:
story.find().populate("fans");
Comments
Post a Comment