猫鼬重复与唯一的架构键 [英] Mongoose duplicates with the schema key unique

查看:68
本文介绍了猫鼬重复与唯一的架构键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使关键项目在整个馆藏中独一无二,但是我无法使它正常工作,我在这里发现了类似的问题.

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

task.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

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

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

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

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") }

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

// 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?

推荐答案

您传递的Schema对象可能无法正常工作,因为您将"unique"属性嵌套到"index"属性中,请尝试类似的操作(它可以预期):

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

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

注意:我不知道您要使用"dropDups"参数做什么,它似乎不在

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.

这篇关于猫鼬重复与唯一的架构键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆