Using mongoose, if I have a Note
model, I can retrieve paginated and sorted results using query options on the find
function, like so...
Note.find({ creator: creatorId})
.select('text')
.limit(perPage)
.skip(perPage * page)
.sort({
name: 'asc'
})
.exec(function(err, notes) {
Note.count().exec(function(err, count) {
res.render('notes', {
notes: notes,
page: page,
pages: count / perPage
})
})
});
Can I achieve the same functionality (filter, select, limit, skip, sort etc) if I embed the Note
schema within a parent document (notesContainerSchema
) like so:
var noteSchema = new Schema({
creator: { type: String },
text: { type: String }
});
var notesContainerSchema = new Schema({
key: { type: String, unique: true },
notes: [ noteSchema ] // note schema is now an array of embedded docs
});
var NotesContainer = db.model('notesContainer', notesContainerSchema);