Sails.js &MongoDB:重复键错误索引 [英] Sails.js & MongoDB: duplicate key error index

查看:50
本文介绍了Sails.js &MongoDB:重复键错误索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Sails.js (0.9.8) 和 MongoDB(通过sails-mongo 适配器)来创建可以定位在树视图中的页面集合.我想将页面的路径存储在 UUID 数组中

I'm using Sails.js (0.9.8) and MongoDB (via the sails-mongo adaptor) to create a collection of pages that can be positioned in a tree-view. I would like to store the path of a page in an array of UUIDs

我的模型:

module.exports = {    
    schema: true,
    attributes: {
        uuid: {
            type: 'string',
            unique: true,
            required: true,
            uuidv4: true
        },
        name: {
            type: 'string',
            required: true,
            empty: false
        },
        path: {
            type: 'array',
            required: true,
            array: true
        }
    }
}

当我保存根"页面时效果很好(路径"属性只有一项,因为它是根页面.这是保存在 MongoDB 中的内容:

It works well when I save a 'root' page (the 'path' property has just one item because it's a root page. Here is what it was saved in MongoDB:

{
    _id: ObjectId("52f853e9609fb6c0341bdfcc"),
    createdAt: ISODate("2014-02-10T04:22:01.828Z"),
    name: "Home Page",
    path: [
        "a2b23e1f-954b-49a3-91f1-4d62d209a093"
    ],
    updatedAt: ISODate("2014-02-10T04:22:01.833Z"),
    uuid: "a2b23e1f-954b-49a3-91f1-4d62d209a093"
}

但是,当我想在之前创建的页面(主页/产品)下方创建一个子页面"时,出现此错误:

But when I want to create a 'subpage' below my previous created page (Home Page/Products), I get this error:

MongoError: E11000 重复键错误索引:cms-project.item.$path_1复制密钥:{ : "a2b23e1f-954b-49a3-91f1-4d62d209a093" }

MongoError: E11000 duplicate key error index: cms-project.item.$path_1 dup key: { : "a2b23e1f-954b-49a3-91f1-4d62d209a093" }

这是我发送的数据:

{ name: 'Products',
  uuid: 'a004ee54-7e42-49bf-976c-9bb93c118038',
  path: 
   [ 'a2b23e1f-954b-49a3-91f1-4d62d209a093',
     'a004ee54-7e42-49bf-976c-9bb93c118038' ] }

我可能错过了一些东西,但我不知道是什么.如果我将路径存储在字符串而不是数组中,它运行良好,但我发现它不那么优雅和方便.

I probably missed something but I don't know what. If I store the path in a string instead of an array, it work well, but I find it much less elegant and handy.

推荐答案

我自己不确定所有的帆/水线部分,因为我从来没有玩过它.但是由于错误,问题是您的数组字段上有一个唯一索引.

Not sure of all the Sails / Waterline parts myself as I've never played with it. But by the error the problem is there is a unique index on your array field.

当您插入第二个文档时,您在另一个文档的路径字段中已经有一个值(父).唯一约束不允许这样做.最肯定的是,对于您正在建模的内容,您不想要这个并且索引不能是唯一的.

When you are inserting your second document, you already have one of the values (the parent) in your path field in another document. The unique constraint is not going to allow this. Most certainly for what you are modelling, you do not want this and the index cannot be unique.

我希望你自己设置这个假设它意味着在文档中包含的数组中是唯一的.如果您这样做了,那么您现在就知道该往哪里看以及该更改什么.如果这以某种方式自动部署,那么我不是要帮忙的人.

I hope that you set this up yourself under the assumption that it meant unique within the array contained in the document. If you did then you know where to look and what to change now. If this is being automatically deployed somehow, then I'm not the one to help.

将索引更改为不唯一.您可以通过 mongo shell 确认这一点:

Change the index to not be unique. You can confirm this through the mongo shell:

use cms-project
db.item.getIndices()

祝你好运

这篇关于Sails.js &MongoDB:重复键错误索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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