Im new to all this and need a little help please:

I have two Mongodb collections (Im using mongoose), Clients and Options.

They are not linked to each other.

I need to access both sets f data on one page in another app.

I can create an API Route pointing to each of these collections:

Clients: https://example.com/api/clients

Options: https://example.com/api/options

But what is the best way to merge these two into one API Route eg:

https://example.com/api/clients_options (URL is irrelevant I just need both sets of data Clients & Options to be accessible from both.)

Here is my Clients Model/Schema:

const ClientSchema = new mongoose.Schema({
    client: String,
    brands: [
        {
         type: mongoose.Schema.Types.ObjectId,
         ref: 'Brand'
        }
    ]

});

module.exports = mongoose.model('Client', ClientSchema);

here is my Options Model/Schema:

const OptionsSchema = new mongoose.Schema({
    agencies: Array,
    asset_types: Array,
    format: Array,
});

module.exports = mongoose.model('Options', OptionsSchema);

I was thinking the best way to do this is to create a master Model/Schema that wraps both the Clients and Options schemas the way the Client Model/Schema wraps the Brand Schema.

eg:

const MasterSchema = new mongoose.Schema({
    clients: {
         type: mongoose.Schema.Types.ObjectId,
         ref: 'Clients'
       },
    options: {
         type: mongoose.Schema.Types.ObjectId,
         ref: 'Options'
       }

});

module.exports = mongoose.model('Master', MasterSchema);

But when I do this, my new Masters Model/Schema doesn't seem to populate with the existing Client and Option Model/Schemas, so my new Master Model/Schema stays blank and doesn't get created as a collection?

Is there a better way to do this?

These are my Routes for each:

//CLIENT API
    app.get('/api/clients',function(req, res){
        Client.find({})
            .populate({
                path: 'brands',
                populate: {
                    path: 'campaigns',
                    model: 'Campaign'
                } 
            }).exec(function(err, clients){
                if(err) {
                    console.log('ERROR!');
                } else {
                    let data = [];
                    clients.forEach(client => {

                        //console.log(client);
                        data.push(client);
                    });
                    res.send({clients: data});
                }
            });
    });
    //OPTIONS API
    app.get('/api/options',function(req, res){
        Options.find({})
            .exec(function(err, options){
                if(err) {
                    console.log('ERROR!');
                } else {
                    let data = [];
                    options.forEach(option => {

                        //console.log(client);
                        data.push(option);
                    });
                    res.send({options: data});
                }
            });
    });

I would like to merge the data of the two into a single api so that I can call only one API URL in another app to access all the data, but cant figure out how/the best way to do it?

1 Answers

0
GNova On Best Solutions

Ive managed to figure it out, I did it in the following way:

//ALL DATA API
    app.get('/api/data',function(req, res){
        Client.find({})
            .populate({
                path: 'brands',
                populate: {
                    path: 'campaigns',
                    model: 'Campaign'
                } 
            }).exec(function(err, clients){
                if(err) {
                    console.log('ERROR!');
                } else {
                    let clientData = [];
                    clients.forEach(client => {

                        //console.log(client);
                        clientData.push(client);
                    });

                    Options.find({})
                        .exec(function(err, options){
                            if(err) {
                                console.log('ERROR!');
                            } else {
                                let optionsData = [];
                                options.forEach(option => {

                                    //console.log(client);
                                    optionsData.push(option);
                                });
                                res.send({clients: clientData, options: optionsData});
                            }
                        });
                }
            });
    });