在MongooseJS中创建模型时的第三个参数 [英] Third argument when creating model in MongooseJS
问题描述
最近几天,我花了很多时间学习MongoDB和MongooseJS,而最适合我的技术是阅读代码和大量代码.今天,我偶然发现了两个有趣的东西,这些东西没有任何文档覆盖,或者也许是,我只是找不到合适的部分.
我想问的第一件事是 ref
属性-它的作用是什么?(我知道它是offtopic,但是当我像下面的示例中那样设置 index
属性时,它会有助于按用户搜索标签吗?)
var TagSchema = new Schema({...用户:{类型:Schema.ObjectId,参考:用户",索引:正确},...});
第二(或第三)件事是-最后一个参数在做什么?
mongoose.model('Tag',TagSchema,'tags');
它们都在文档中:
ref
是Mongoose对参考人口的支持的一部分./p>
mongoose.model
的第三个参数是一个明确的集合名称.
Last few days I spent learning MongoDB and MongooseJS, and the best technique that works for me is reading code, lots of code. Today I've stumbled upon on two interesting things that are not covered by any documentation, or maybe they are and I just can't find the right section.
First thing I want to ask about is that ref
property - what is it for? (I know it is offtopic, but when I set index
property like in example below, will it help in searching Tags by User?)
var TagSchema = new Schema({
...
user: {
type: Schema.ObjectId,
ref: 'User',
index: true
},
...
});
Second (or third) thing is - what is doing that last argument?
mongoose.model('Tag', TagSchema, 'tags');
They're both in the documentation:
ref
is part of Mongoose's support for reference population.
The third parameter to mongoose.model
is an explicit collection name.
这篇关于在MongooseJS中创建模型时的第三个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!