Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

mongodb - Mongoose -- Force collection name

I am trying to use mongoose to create a database and a collection in it. My code is:

var mongoose = require('mongoose');
    var db = mongoose.connect('mongodb://localhost/testdb');
    var Schema = mongoose.Schema;

    var UserInfo = new Schema({
    username : String,
    password : String 
    });

    mongoose.model('UserInfo', UserInfo);

    var user = db.model('UserInfo');


    var admin = new user();
    admin.username = "sss";
    admin.password = "ee";
    admin.save();

When I run this code, mongoose created collection named UserInfo instead of userinfo. How to force collection name in mongoose?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should do it

var UserInfo = new Schema({
  username : String,
  password : String 
}, { collection: 'userinfo' });

See this link from the Mongoose documentation for more information.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...