160

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?

Eliott Paris
  • 149
  • 1
  • 10
ravi
  • 1,601
  • 2
  • 11
  • 3

9 Answers9

255

This should do it

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

See this link from the Mongoose documentation for more information.

gandreadis
  • 3,004
  • 2
  • 26
  • 38
Thomas Blobaum
  • 3,680
  • 1
  • 17
  • 16
  • 7
    Thanks for this. Here's the relevant link: http://mongoosejs.com/docs/guide.html#collection – Jon Page Dec 07 '14 at 20:23
  • 2
    Thanks @JonPage, I was wondering why my collection of Person objects was named as people. The link you provided is a good reference. I think this method of setting a custom collection name when creating a schema is better than the other method of defining a collection name when creating the model. – Bharat Mar 10 '15 at 04:22
  • This was super helpful. I spent two days trying to query something now. – Holt Mansfield Sep 26 '18 at 15:23
105

If you are using mongoose 2.0.0, pass the collectionName as the third argument

mongoose.model('UserInfo', UserInfo, 'UserInfo');
Bilal Husain
  • 1,730
  • 1
  • 12
  • 11
36

Mongoose will add 's' to collection name by default. If you want to avoid that, just pass as third argument the name of the collection:

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, 'UserInfo')

tan = new user();
admin.username = 'sss';
admin.password = 'ee';
admin.save();
gandreadis
  • 3,004
  • 2
  • 26
  • 38
vijay kumar
  • 1,025
  • 13
  • 11
  • 1
    This is the best suggestion. I have always found it the easiest to just pass the collection name in mongoose.model(). Also good for pointing out how mongoose pluralizes the collection name, that was confusing when I first started. – John Morrison Mar 24 '17 at 17:10
17

API structure of mongoose.model is this:

Mongoose#model(name, [schema], [collection], [skipInit])

What mongoose do is that, When no collection argument is passed, Mongoose produces a collection name by pluralizing the model name. If you don't like this behavior, either pass a collection name or set your schemas collection name option.

Example:

var schema = new Schema({ name: String }, { collection: 'actor' });

or

schema.set('collection', 'actor');

or

var collectionName = 'actor'
var M = mongoose.model('Actor', schema, collectionName);
Simran
  • 2,364
  • 1
  • 12
  • 19
11

You need to set the collection name in your schema.

new Schema({...},{collection: 'userInfo'});
James Freund
  • 473
  • 4
  • 13
9

Mongoose maintainer here. We recommend doing mongoose.model('UserInfo', UserInfo, 'UserInfo');, third arg to mongoose.model() is the collection name. Here's the relevant docs.

vkarpov15
  • 3,614
  • 24
  • 21
8

Passing a third argument on module.exports = mongoose.model('name', schema, 'collection') overrides the automatic collection name based on model name, which has already been answered.. but there are 2 other ways,

per mongoose.model doco link: https://mongoosejs.com/docs/api.html#mongoose_Mongoose-model

there are 3 methods to manually enter a collection name:

var schema = new Schema({ name: String }, { collection: 'actor' });

// or

schema.set('collection', 'actor');

// or

var collectionName = 'actor'
var M = mongoose.model('Actor', schema, collectionName)
5

Answer:

mongoose.model('UserInfo', UserInfo, 'userinfo'); //3rd parameter 'userinfo': as collection name

Better explanation with syntax:

Mongoose.model(name, [schema], [collection], [skipInit])

Parameters Explanation:

  • 1st parameter - name model name
  • 2nd parameter [schema] schema name
  • 3rd parameter [collection] collection name (optional, induced from model name)
  • 4th parameter [skipInit] whether to skip initialization (defaults to false)
Bijay Pal
  • 241
  • 3
  • 5
0

your model name : userInfo.js

in express route file or app.js

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

then in your userInfo.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserInfo = new Schema({
 username : String,
 password : String 
});
module.exports = mongoose.model('UserInfo', UserInfo);