简体   繁体   English

猫鼬架构参考和填充

[英]Mongoose Schema Ref & Populate

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' }] . fans: [{ type: Number, ref: 'Person' }]

Have you tried the following?: 您是否尝试过以下方法?

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

Also, the name of your model is 'calendarlist'. 另外,模型的名称为“ calendarlist”。 Is your collection called 'calendarlists'? 您的收藏集称为“日历列表”吗? Because 'Mongoose automatically looks for the plural version of your model name' per this link . 因为“猫鼬会自动通过此链接查找您模型名称的复数形式”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM