Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 905
ravi Avatar asked Sep 20 '11 14:09

ravi


People also ask

Does Mongoose model create a collection?

Mongoose never create any collection until you will save/create any document.

What is collection name in MongoDB?

Naming convention of MongoDB Collection The name of the collection will be a UTF-8 character string. collection name will not start with system, because system. is a reserved keyword, e.g. system. users hold user database. collection in MongoDB is unique, likewise, the table in the database is unique.

What is versionKey in Mongoose?

The versionKey is a property set on each document when first created by Mongoose. This keys value contains the internal revision of the document. The name of this document property is configurable.

Is Mongoose ODM or ORM?

Mongoose is an ODM that provides a straightforward and schema-based solution to model your application data on top of MongoDB's native drivers.


8 Answers

This should do it

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

See this link from the Mongoose documentation for more information.

like image 183
Thomas Blobaum Avatar answered Sep 29 '22 00:09

Thomas Blobaum


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

mongoose.model('UserInfo', UserInfo, 'UserInfo');
like image 30
Bilal Husain Avatar answered Sep 29 '22 00:09

Bilal Husain


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();
like image 25
vijay kumar Avatar answered Sep 28 '22 22:09

vijay kumar


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);
like image 25
Simran Avatar answered Sep 28 '22 22:09

Simran


You need to set the collection name in your schema.

new Schema({...},{collection: 'userInfo'});
like image 35
James Freund Avatar answered Sep 28 '22 23:09

James Freund


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.

like image 28
vkarpov15 Avatar answered Sep 28 '22 22:09

vkarpov15


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)
like image 22
Topher Chapman Avatar answered Sep 29 '22 00:09

Topher Chapman


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)
like image 23
Bijay Pal Avatar answered Sep 28 '22 23:09

Bijay Pal