0

Okay so I have an events.js file with an eventSchema:

var eventSchema = mongoose.Schema({
name:{
    type: String,
    //required: true
venue:{

}
});

and a venue.js with the venue schema:

var mongoose = require('mongoose');

var venueSchema = mongoose.Schema(
{
name:{
    type: String,
    //required: true
},
postcode:{
    type: String,
    //required: true
},
town:{
    type: String,
    //required: true
}
});

My question is how can I have the 'venue' field in the events schema be linked to the venue schema. So essentially when you create a new event you can only add a venue from the list of venues. Thanks in advance!

2 Answers2

1

You can link it by id. You don't have to add all 'venue' field in the events schema.

var eventSchema = mongoose.Schema({
  name:{
    type: String,
    //required: true
  },
  venue_id: Schema.Types.ObjectId,
});

Since you're using Mongoose, You can make field like,

venue_id: { type: Schema.Types.ObjectId, ref: 'Venue' }

which uses populate method.

Population is the process of automatically replacing the specified paths(which is venue_id) in the document with document(s) from other collection(s) (which is document matched with venue_id).

You can use it like

event.
  findOne({ name: 'somename' }).
  populate('venue').
  exec(function (err, event) {
    if (err) return handleError(err);
});

It will return event doc with venue doc that match with venue_id, not just venue_id.

godsenal
  • 387
  • 2
  • 12
  • is this the correct syntax? venue:{ venue_id: { type: Schema.Types.ObjectId } } –  Nov 22 '17 at 00:14
  • venue:{ venue_id: { type: Schema.Types.ObjectId } } is not the correct syntax. What you want to do is make reference of venue schema. You can make venue_id field which holds venue collection's id like my answer. Or if you want to make more specific, create field like venue_id: {{type: mongoose.Schema.Types.ObjectId, ref: 'Venue'}. – godsenal Nov 22 '17 at 00:22
  • Could you please explain a bit more how the populate method works? How should I use it in this instance. Thanks!! –  Nov 22 '17 at 16:02
  • Thank you. How does this work in relation to POST requests, i.e. can I simply do POST: {"name":"eventName", {venue:"venue_id"}} and it will return the venue? Cheers! –  Nov 23 '17 at 13:37
  • Are you asking about inserting doc ? You don't have to wrap venue field in bracket. Just {name: "eventName", venue_id: "venue_id} – godsenal Nov 23 '17 at 15:01
0

The answer given by @godsnam is correct but when you want to use references in your schema. If you don't want to do it using references then this might help you.

You can do link your venueSchema in eventSchema by simply writing as below.

var venueSchema = mongoose.Schema(
    {
        name: {
            type: String,
            //required: true
        },
        postcode: {
            type: String,
            //required: true
        },
        town: {
            type: String,
            //required: true
        }
    });

var eventSchema = mongoose.Schema({
    name: {
        type: String,
        //required: true
    },
    venue: venueSchema
});
Pavan Vora
  • 1,634
  • 14
  • 19
  • How do I do this given that the venueSchema is in another file (in the same folder)? –  Nov 22 '17 at 15:30
  • You can do require(schemaPath); – Pavan Vora Nov 22 '17 at 15:56
  • Okay so i've done: Venue = require('./venues'); in the events.js file and then in the eventSchema i've added: venue: venueSchema but in the terminal I get referenceError: venueSchema is not defined :/ –  Nov 22 '17 at 15:58
  • Do venueSchema = require(). And inside venueSchema file do module.exports = your venueSchema – Pavan Vora Nov 22 '17 at 16:05
  • Thanks a lot! that seems to work! Although do you know why when I do a GET request on the events the venue name becomes really weird, like this: "0": "0", "1": "2", "2": " ", "3": "A", "4": "c", "5": "a", "6": "d", "7": "e", "8": "m", "9": "y", when I do a GET request on the venue it just says name:02academy –  Nov 22 '17 at 16:17
  • i'm still a bit confused - how do you refer to the venue when you make a POST request - how do you fill it in "venue":"" –  Nov 22 '17 at 16:30
  • Complete object which have name, postcode and town in it. – Pavan Vora Nov 22 '17 at 16:37
  • Okay but I already have venues that I want to add to the events, rather than fill in all the venue details - is that possible? –  Nov 22 '17 at 16:40
  • Not getting what you are saying. – Pavan Vora Nov 22 '17 at 17:01
  • Well on my app I want to be able to add venues to events, and add events to venues. How do I code it so that I create venues separately (with all their details), then link these venues to the events without repeating the details? Cheers. –  Nov 22 '17 at 17:07
  • If I got you correctly then I think you should go with referencing. Here how you can do reference and populate. http://mongoosejs.com/docs/populate.html – Pavan Vora Nov 22 '17 at 17:12
  • Okay so i have venue: {type: mongoose.Schema.Types.ObjectId, ref: 'Venue'} and Post.findOne({_id: }) .populate('venue') .exec(function(err,post)){ }); what should i put in the function & the _id:? –  Nov 22 '17 at 17:28