Third argument when creating model in MongooseJS

ε祈祈猫儿з 提交于 2021-01-05 06:12:27

问题


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');

回答1:


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.



来源:https://stackoverflow.com/questions/26390448/third-argument-when-creating-model-in-mongoosejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!