[英]Mongoose Schema Ref & Populate
我想用同一集合中的一組文檔填充數據庫查詢。 在從未引用之前,我無法確定錯誤是在模型還是在查詢中。
我當前的架構如下所示,
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)
我的樣本文檔看起來像這樣,
{ "_id" : "mixtest", "mixin" : [ { "$ref" : "calendarlist", "$id" : "cVkKRkNtB-" } ], "title" : "mix test", "__v" : 3, "notes" : [] } and { "_id" : "cVkKRkNtB-", "title" : "found doc", "__v" : 3, "notes" : [] }
我的查詢看起來像這樣,
calendarlist.find({}).populate('mixin').sort({ title: 1 }).exec(function(err, s) { if (err) {console.log(err) } console.log(s)})
完全贊賞建議,技巧或一般性說明。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.