I would like to populate my database query with a set of documents from the same collection. Having never referenced before I can't tell if the error is in my model or in my query.
My current schema looks like this,
var mongoose = require('mongoose'), Schema = mongoose.Schema, shortid = require('shortid') /* Calendar Schema */ var CalendarSchema = mongoose.Schema({ _id: { type: String, unique: true, 'default': shortid.generate }, title:String, mixin: [{_id: { type: String, ref: 'calendarlist' }}], notes: Array }) module.exports = mongoose.model('calendarlist', CalendarSchema)
My sample docs looks like this,
{ "_id" : "mixtest", "mixin" : [ { "$ref" : "calendarlist", "$id" : "cVkKRkNtB-" } ], "title" : "mix test", "__v" : 3, "notes" : [] } and { "_id" : "cVkKRkNtB-", "title" : "found doc", "__v" : 3, "notes" : [] }
and my query looks like this,
calendarlist.find({}).populate('mixin').sort({ title: 1 }).exec(function(err, s) { if (err) {console.log(err) } console.log(s)})
Suggestions, tips or general clarifications totally appreciated. Thanks.
This link shows that the way to do reference is for example like below:
fans: [{ type: Number, ref: 'Person' }]
.
Have you tried the following?:
mixin: [ { type: String, ref: 'calendarlist' }],
Also, the name of your model is 'calendarlist'. Is your collection called 'calendarlists'? Because 'Mongoose automatically looks for the plural version of your model name' per this link .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.