简体   繁体   English

我可以使用mongodb ref引用同一模型中的模型吗

[英]Can I use the mongodb ref to reference a model in that same model

Let assume I have a model called Task that has a field called dependencies and the dependencies field is of type mongodb ID and I will like it to be a reference to the Task model itself. 假设我有一个名为Task的模型,该模型具有一个称为dependencies的字段,并且dependencies字段的类型为mongodb ID,我希望它可以作为对Task模型本身的引用。 I don't know if this is possible with mongodb and mongoose. 我不知道mongodb和mongoose是否可行。

For Example: 例如:

// Task model
const taskSchema = mongoose.Schema({
// other fields

// dependencies: [{
  types: mongoose.Schema.Types.ObjectId,
  ref: 'Task' // is this possible?at what cost?
}]

})

modules.export = mongoose.model('Task', taskSchema)

Yes, nesting is possible in the MongoDB and in the mongoose as well. 是的,在MongoDB和Mongoose中也可以嵌套。 Try to avoid depth. 尽量避免深度。

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

相关问题 如何引用另一个文件夹中的模型? - how can I ref a model that is in another folder? 我可以使用 MongoDB 模式模型来定义 IndexedDB 索引吗? - Can I use MongoDB schema model for defining IndexedDB indexes? 如何在创建集合时根据同一 model 的属性设置属性? Nodejs - Mongodb - How can I set a property depending on properties of the same model on collection creation ? Nodejs - Mongodb 如何将 ref 用于循环中的同一组件? - How can I use ref for the same component which is in the loop? 我可以在mongoose.js模型中而不是每次查询时填充引用吗? - Can I populate ref's in a mongoose.js model instead of everytime I query? 猫鼬 | NodeJS:如何将两个“ref”分配给相同的模型术语并填充它? - Mongoose | NodeJS : How to assing two 'ref' to same model term and populate it? 如何在获取 model 数据时填充它?Mongodb - How can i populate a model data while fetching it?Mongodb 我可以在不创建模型的情况下访问 mongodb 集合吗? (猫鼬) - can I reach mongodb collections without creating model? (mongoose) 在 mongoDB 中创建期间,我可以将元素推送到模型的数组中吗? - Can I push an element into a model's array during creation in mongoDB? 我想更新许多 model A 然后更新另一个参考 model B 基于更新的 model A - I want to updateMany a model A then update another ref model B base on updated model A
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM