Friday, 15 January 2010

node.js - How to reference another schema in my Mongoose schema? -


I am creating a MonoSize schema for a dating application

Let me know every The person document contains the context of all the events where event is another schema with its own model. How can I describe this in schema?

  var personchem = mongoose.Schema ({firstname: string, last name: string, email: string, gender: {type: string, enum: ["male", "woman"]} Dob: date, city: string, interests: [keys by interest], events attached: ???});  

You describe it using

Population is the process of automatically transcribing the routes specified in the document with the document (s) from another collection (s)

Assume that your The event schema is defined as:

ex> var mongoose = requires ('mongos'), schema = Mongoose . Chemera Event Event Schema = Schema ({title: string, location: string, start date: date, expiry date: date}); Var person schema = schema ({first name: string, last name: string, email: string, gender: {type: string, enum: ["male", "woman"]} dob: date, city: string, interests: [Interests], Events include: [{Type: schema type: object, ref: 'event'}]}); Var event = mongos Model ('event', event sachima); Var person = Mongoz Model ('person', person schema);

To show how populated is used, first person object, Aaron = new person ({first name: 'Aaron'}) and code > Event 1 = New Event ({title: 'Hackathon', location: 'foo'}) :

  aaron.eventsAttended.push (event1); Aaron.save (callback);  

Then, when you create your query, you can populate such references:

  person .findOne ({first name: 'Aaron'}). Populate ('Event Attached') // only works when we rifle a person. Events Eteted XAC (function (fault, person) {if return) returns handler; console.log (person);});  

No comments:

Post a Comment