Inner Queries of References

5 views
Skip to first unread message

Alex Price

unread,
Jun 28, 2013, 1:00:00 PM6/28/13
to mongoo...@googlegroups.com
Hey,
I am trying to figure out how to do an Innery Query. Here are my Schemas:

var Course = mongoose.model('Course',new Schema({

  createdAt: Date,

  updatedAt: { type: Date, 'default': Date.now },

  city: String,

  country: String,

  description: String,

  latitude: String,

  longitude: String,

  name: String,

  status: Boolean,

  Professor_idProfessor:{ type: Schema.Types.ObjectId, ref: 'User' },

  School_idSchool:{ type: Schema.Types.ObjectId, ref: 'School'}

}));


var Section = mongoose.model('Section',new Schema({

  createdAt: Date,

  updatedAt: { type: Date, 'default': Date.now },  

  days: String,

  name: String,

  status: Boolean,

  studentCount: Number,

  time: String,
  Course_idCourse:{ type: Schema.Types.ObjectId, ref: 'Course' }
}));


I want to find all sections who's parent course have the name 'Math'. I figured it would work like this:
Section.find({'Course_idCourse.name':'Math'},function(err,obj){
console.log(err,obj);
});

This does not work though. Any suggestions?

Jason Crawford

unread,
Jun 28, 2013, 3:21:08 PM6/28/13
to mongoo...@googlegroups.com
That query would work if you had an embedded document. But when these are two different collections, you're basically trying to do a join.

As I understand it, there are no joins in MongoDB. You can't do a single query and get data from two different collections. You have to query the first, then separately query the second.

However, Mongoose can help you with this, using population:

Hope that helps,
Jason

--
Blog: http://blog.jasoncrawford.org  |  Twitter: @jasoncrawford



--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages