簡體   English   中英

貓鼬架構參考和填充

[英]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)}) 

完全贊賞建議,技巧或一般性說明。 謝謝。

該鏈接顯示參考方法例如如下:

fans: [{ type: Number, ref: 'Person' }]

您是否嘗試過以下方法?

mixin: [ { type: String, ref: 'calendarlist' }],

另外,模型的名稱為“ calendarlist”。 您的收藏集稱為“日歷列表”嗎? 因為“貓鼬會自動通過此鏈接查找您模型名稱的復數形式”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM