I am new on mongoose, i tried to insert some datas, but when i look on rockmongo, the model supposed to be “User” is “users”, “Stat” is “stats” etc…
Here are the Schemas:
var UserSchema = new mongoose.Schema({
username: String,
facebook_id: String,
facebook_token: String,
img: String,
email: String,
created_at: { type: Date, default: Date.now }
});
var StatSchema = new mongoose.Schema({
user_id: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
detail: {
right: [{type: mongoose.Schema.Types.ObjectId, ref: 'Question'}],
wrong: [{type: mongoose.Schema.Types.ObjectId, ref: 'Question'}],
score: Number
},
created_at: { type: Date, default: Date.now }
});
var User = db.model('User', UserSchema);
var Stat = db.model('Stat', StatSchema);
and this code create the “users” and “stats” collections:
var U1 = new User({ username: "toto", facebook_id: req.params.facebook_id, facebook_token: req.params.facebook_token })
U1.save(function (err) {
if (err)
console.log('U1 save error');
var S1 = new Stat({ user_id: U1.id })
S1.save(function (err) {
if (err)
console.log('S1 save error');
});
});
Why ? Thanks for your help !
Because the default behavior is to use the lower-cased, pluralized version of the model name as mostly described in the docs (it doesn’t mention the lower-cased part).
You can provide your own collection name as a third parameter to
modelif you want something else: