“Join” two collection in MongoDB using node js

点点圈 提交于 2019-12-11 03:13:13

问题


I'm trying to make a query between two collections in mongodb using node js.

I have these two collection in the 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 the route, I do I query according to the id that step and I would like that the query I would restore a json like this:

Result i 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 this route :

/*
TRIP BY 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 but if you were to make multiple calls simultaneously, no longer work.

I have an idea: count the number of requests and go to double check if the id of the place is in this trip but it seems very expensive. I would like to know a more practical method for making queries with multiple collections in mongo. Thanks in advance


回答1:


first i recommend you use mongoose. Before all you must prepare your Models, for 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 this when you want "join" you most use next method:

Story.find().populate("fans");

For moar http://mongoosejs.com/docs/populate.html




回答2:


Just get the basic JSON data on first request

{ "_id":ObjectId("55a922531e35772c1b17d4a0"), "name":"trip_name", "waypoints":[ "4c828999d8086dcb03877752", "4dd2ae657d8b4c6585f1a6fd", "4c59c3e4f346c928a8634dca" ] }

Then call other API which gets data of waypoints using async calls and promise and once promises are resolved, merge them.



来源:https://stackoverflow.com/questions/31480088/join-two-collection-in-mongodb-using-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!