node.js Mongoose 重复,模式键唯一

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

Mongoose duplicates with the schema key unique

node.jsmongoose

提问by Risto Novik

I want to make the key project unique across that collection but i cant getting this working, i found similar problem here.

我想让关键项目在该集合中独一无二,但我无法使其正常工作,我在这里发现了类似的问题。

task.js

任务.js

function make(Schema, mongoose) {

    var Tasks = new Schema({
        project: { type: String, index: { unique: true, dropDups: true }},
        description: String
    });

    mongoose.model('Task', Tasks);
}
module.exports.make = make;

test.js

测试.js

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

var Schema = mongoose.Schema
  , ObjectId = Schema.ObjectId;

require('./task.js').make(Schema, mongoose);
var Task = mongoose.model('Task');
var newTask = new Task({
    project: 'Starting new project'
  , description: 'New project in node'
});
newTask.save(function(err) {
    if (err) console.log('Error on saving');
});

mongoose.disconnect();

When i run the app with node test.js, still creates duplicates.

当我使用 node test.js 运行应用程序时,仍然会创建重复项。

MongoDB shell version: 2.0.2
connecting to: rss
> db.tasks.find()
> db.tasks.find()
{ "project" : "Starting new project", "description" : "New project in node", "_id" : ObjectId("4f21aaa3d48d4e1533000001") }
{ "project" : "Starting new project", "description" : "New project in node", "_id" : ObjectId("4f21aaa4d9a8921a33000001") }
{ "project" : "Starting new project", "description" : "New project in node", "_id" : ObjectId("4f21aaa57ebeea1f33000001") }

// Edit still same problem, here's what i tried to do delete the db.tasks.drop() collection restart mongo sudo stop mongodb and start mongodb, ran the program again and still same problem, how does it allow duplicate data on index?

// 编辑仍然是同样的问题,这是我试图做的删除 db.tasks.drop() 集合重新启动 mongo sudo 停止 mongodb 并启动 mongodb,再次运行程序,仍然是同样的问题,它如何允许索引上的重复数据?

回答by Arnaud Rinquin

The Schema object you're passing may not work correctly because you are nesting 'unique' attribute into 'index' attribute, try something like this (it works as intended) :

您传递的 Schema 对象可能无法正常工作,因为您将“唯一”属性嵌套到“索引”属性中,请尝试以下操作(按预期工作):

User = mongoose.model('User', new Schema({
    firstName:  {
        type:String,
        required: true,
    },
    lastName: {
        type:String,
        required: true,
    },
    email: {
        type:String,
        required: true,
        unique: true
    },
    address: String,
    phone: {
        type:String,
        required: true,
    },
    password:  {
        type:String,
        required: true,
        set: Data.prototype.saltySha1 // some function called before saving the data
    },
    role: String
},{strict: true}));

Or more specifically for your example :

或更具体地说,对于您的示例:

var Tasks = new Schema({
    project: { 
        type: String, 
        unique: true,
        index: true
    },
    description: String
});

Note : I don't know what you're trying to do with the "dropDups" parameter, it doesn't seems to be in the mongoose documentation.

注意:我不知道你想用“dropDups”参数做什么,它似乎不在猫鼬文档中