mongodb Mongoose -- Force 集合名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7486528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:14:57  来源:igfitidea点击:

Mongoose -- Force collection name

mongodbmongoose

提问by ravi

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

我正在尝试使用 mongoose 在其中创建一个数据库和一个集合。我的代码是:

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?

当我运行此代码时,猫鼬创建了名为 UserInfo 而不是 userinfo 的集合。如何在猫鼬中强制集合名称?

回答by Thomas Blobaum

This should do it

这应该做

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

See this linkfrom the Mongoose documentation for more information.

有关更多信息,请参阅Mongoose 文档中的此链接

回答by Bilal Husain

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

如果您使用的是 mongoose 2.0.0,请将 collectionName 作为第三个参数传递

mongoose.model('UserInfo', UserInfo, 'UserInfo');

回答by vijay kumar

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:

默认情况下,Mongoose 会在集合名称中添加“s”。如果你想避免这种情况,只需将集合的名称作为第三个参数传递:

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();

回答by Sam

API structure of mongoose.model is this:

mongoose.model 的 API 结构是这样的:

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.

mongoose 所做的是,当没有传递集合参数时,Mongoose 通过将模型名称复数化来生成集合名称。如果您不喜欢这种行为,请传递一个集合名称或设置您的架构集合名称选项。

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);

回答by James Freund

You need to set the collection name in your schema.

您需要在架构中设置集合名称。

new Schema({...},{collection: 'userInfo'});

回答by vkarpov15

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.

猫鼬维护者在这里。我们建议这样做mongoose.model('UserInfo', UserInfo, 'UserInfo');,第三个参数mongoose.model()是集合名称。这是相关的文档

回答by Bijay Pal

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)
  • 第一个参数 - 名称模型名称
  • 第二个参数 [schema] 架构名称
  • 第三个参数 [collection] 集合名称(可选,从模型名称导出)
  • 第4个参数[skipInit]是否跳过初始化(默认为false)

回答by Shyam Kumar Nallaguntla

your model name : userInfo.js

您的模型名称:userInfo.js

in express route file or app.js

在快速路由文件或 app.js 中

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

then in your userInfo.js

然后在你的 userInfo.js

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