问题
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