唯一索引不适用于 Mongoose / MongoDB

发布于 2024-11-05 11:24:24 字数 1498 浏览 1 评论 0原文

我在使用 Mongoose / MongoDb 创建唯一索引时遇到问题,无法让它工作。当我设置唯一索引时,我可以添加两个具有相同属性值的文档。

我已经尝试了我能想到的一切 - 重新启动(一切)更改语法等。

代码

Addtion >>

这是我用来保存实体的方法:

  create  : function(entity, definition, successFn, errorFn){

    var model = mongoose.model(entity);
    newModel = new model(definition);

    newModel.save(function(error) {
      if(error){
        if(!errorFn){
          throw error;
        }
        errorFn(newModel);
        return;
      }

      successFn(newModel);
    });
  }...

<<

var Something = new Schema({
  objectId          : ObjectId,
  name              : { type : String, index: { unique: true }}, 
  url               : { type : String, index: { unique: true }},
...etc
mongoose.model('Something', Something);

Mongo 输出

 [conn1] insert xxxxx.agencies 1526ms
 [conn1] building new index on { name: 1 } for xxxxx.agencies
 [conn1] insert xxxxx.system.indexes exception 11000 E11000 duplicate key error    index: xxxxx.agencies.$name_1  dup key: { : "something" } 4ms
 [conn1] building new index on { url: 1 } for xxxxx.agencies
 [conn1] insert xxxxx.system.indexes exception 11000 E11000 duplicate key error index: xxxxx.agencies.$url_1  dup key: { : "http://www.something.com" } 1ms

当我签入 MongoHub 时,索引没有出现,因此它们看起来不像已创建。

这是此问题的重复项,但没有有效的答案为我。

I've got a problem with creating unique indexes using Mongoose / MongoDb and can't get it to work. I'm able to add two documents with the same attribute values when I have set a unique index.

I've tried everything I can think of - restarting (everything) changing the syntax etc.

Code

Addtion >>

This is the method that I'm using to save an entity:

  create  : function(entity, definition, successFn, errorFn){

    var model = mongoose.model(entity);
    newModel = new model(definition);

    newModel.save(function(error) {
      if(error){
        if(!errorFn){
          throw error;
        }
        errorFn(newModel);
        return;
      }

      successFn(newModel);
    });
  }...

<<

var Something = new Schema({
  objectId          : ObjectId,
  name              : { type : String, index: { unique: true }}, 
  url               : { type : String, index: { unique: true }},
...etc
mongoose.model('Something', Something);

Mongo output

 [conn1] insert xxxxx.agencies 1526ms
 [conn1] building new index on { name: 1 } for xxxxx.agencies
 [conn1] insert xxxxx.system.indexes exception 11000 E11000 duplicate key error    index: xxxxx.agencies.$name_1  dup key: { : "something" } 4ms
 [conn1] building new index on { url: 1 } for xxxxx.agencies
 [conn1] insert xxxxx.system.indexes exception 11000 E11000 duplicate key error index: xxxxx.agencies.$url_1  dup key: { : "http://www.something.com" } 1ms

When I checked in MongoHub the indexes don't appear, so they don't look like they have been created.

This is a duplicate of this question, but it doesn't have an answer that works for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水晶透心 2024-11-12 11:24:24

一种不涉及擦除数据库的解决方案是手动删除任何重复项,然后按照以下方式运行某些内容:

db.users.ensureIndex({email:1},{unique:true,sparse:true});

从 mongo shell

one solution that doesn't involve erasing your db, is to remove any duplicates manually, then run something along the lines of:

db.users.ensureIndex({email:1},{unique:true,sparse:true});

from the mongo shell

影子的影子 2024-11-12 11:24:24

看起来索引创建失败是因为 MongoDB 集合中已经存在重复数据。如果可能,请尝试删除所有数据并从空集合重新开始。

It looks as though the indexes are failing to create because there's already duplicate data in the MongoDB collection. If possible, try deleting all the data and start again with an empty collection.

歌入人心 2024-11-12 11:24:24

由于您想在集合中应用唯一索引,因此我建议您删除所有重复的文档。以下是执行此操作的代码:

在 Mongo shell 客户端中:

db.agencies.ensureIndex({name: 1}, {unique: true, dropDups: true});
db.agencies.ensureIndex({url: 1}, {unique: true, dropDups: true});

Since you want to apply unique index in your collection, I suggest you to delete any duplicate documents. Here is the code for doing it:

In Mongo shell client:

db.agencies.ensureIndex({name: 1}, {unique: true, dropDups: true});
db.agencies.ensureIndex({url: 1}, {unique: true, dropDups: true});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文