How to generalise the populate method in MongoDB?

256
November 24, 2017, at 1:09 PM

Basically I want to populate the venue field in the Events schema with all the corresponding venue information (using the venue schema) but I can't work out how to do this. I need it such that when you do a POST request to add a new event, you can add a pre-existing venue to that event with all its information. Thank you!

My code:

var mongoose = require('mongoose')
, Venue = require('./venues');
 //Events schema
var eventSchema = mongoose.Schema({
     name:{
        type: String,
        //required: true
    },
    desc:{
        type: String,
        //required: true
    },
    date:{
        type: Date,
        //required: true
    },
    url:{
        type: String,
        //required: true
    },
    create_date:{
        type: Date,
        default: Date.now
    },
    venue: {type: mongoose.Schema.Types.ObjectId, ref: 'Venue'}
});
var Event = module.exports = mongoose.model('Event', eventSchema);
event.
    findOne({ name: 'somename' }).
    populate('venue').
    exec(function (err,event) {
        if (err) return handleError(err);
});
READ ALSO
topojson command not added on npm install

topojson command not added on npm install

I am getting an error when using topojson saying that the command cannot be foundI think the install has gone OK but there just isn't a command for topojson, but there are ones for topo2geo, topomerge etc

156
How to write JSON array to file in node.js and request it again client side

How to write JSON array to file in node.js and request it again client side

I'm trying to create a drag-and-drop table with save and load functionalityI'm using code from REDIPS

215
Whats the part of Node.js in a Polymer.js setup?

Whats the part of Node.js in a Polymer.js setup?

Why do I have to install nodejs to work with polymer just because of npm? or what is the part of node

223
react-native attempting to resolve built-in fetch function

react-native attempting to resolve built-in fetch function

I'm attempting to use the fetch function in react-native to grab a piece of data from the webfrom what I understand reading the docs, the fetch function is built in to react-native

251