node.js 为什么猫鼬总是在我的收藏名称末尾添加一个 s

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10547118/
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-02 15:40:57  来源:igfitidea点击:

Why does mongoose always add an s to the end of my collection name

node.jsmongodbmongoose

提问by Bob Ren

For example, this code results in a collection called "datas" being created

例如,此代码导致创建一个名为“datas”的集合

var Dataset = mongoose.model('data', dataSchema);

And this code results in a collection called "users" being created

这段代码导致创建一个名为“users”的集合

var User = mongoose.model('user', dataSchema);

Thanks

谢谢

回答by aaronheckmann

Mongoose is trying to be smart by making your collection name plural. You can however force it to be whatever you want:

Mongoose 试图通过使您的收藏名称复数来变得聪明。但是,您可以强制它成为您想要的任何内容:

var dataSchema = new Schema({..}, { collection: 'data' })

var dataSchema = new Schema({..}, { collection: 'data' })

回答by Nishank Singla

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 Andrey Hohutkin

Starting from mongoose 5.x you can disable it completely:

从 mongoose 5.x 开始,您可以完全禁用它:

mongoose.pluralize(null);

回答by Andrea

You can simply add a string as a third argument to define the actual name for the collection. Extending your examples, to keep names as dataand userrespectively:

您可以简单地添加一个字符串作为第三个参数来定义集合的实际名称。扩展您的示例,分别保留名称data和名称user

var Dataset = mongoose.model('data', dataSchema, 'data');

var User = mongoose.model('user', dataSchema, 'user');

回答by sebu

You can add the collection name as third parameter. See the example using Typescript:

您可以添加集合名称作为第三个参数。请参阅使用 Typescript 的示例:

import DataAccess = require('../DataAccess');
import IUser = require("../../Models/Interfaces/IUser");

var mongoose = DataAccess.mongooseInstance;
var mongooseConnection = DataAccess.mongooseConnection;

class UserSchema {
        static get schema () {
        var schema =  mongoose.Schema({
            _id : {
                type: String
            },
            Name: {
                type: String,
                required: true
            },
            Age: {
                type: Number,
                required: true
            }
        });

        return schema;
    }
}
var schema:any = mongooseConnection.model<IUser>("User", 
UserSchema.schema,"User");
export = schema;

回答by Daniel Segura

//Mongoose's definition file. NOT your model files
1 const mongoose = require("mongoose");
2 mongoose.pluralize(null);

Adding the linemongoose.pluralize(null)in your Mongoose file will prevent collection name pluralization. You don't need to add this line to your model files.

mongoose.pluralize(null)在您的 Mongoose 文件中添加该行将阻止集合名称的复数形式。您不需要将此行添加到模型文件中。

As seen here.

这里所见。

回答by Pedro JR

At the end of defining your schema on the next line Use this code

在下一行定义架构的末尾使用此代码

module.exports = mongoose.model("State", "StateSchema", "state")

Assuming that your state is what you want to use on your db to avoid s as states

假设您的状态是您想在数据库上使用的状态以避免 s 作为状态

Click the link to view the picture properly

点击链接正确查看图片