EmbenddedDocuments上的MongoDB UniqueIndex

时间:2017-03-28 08:49:47

标签: mongodb unique-index

我在unique indexes

上创建EmbenddedDocuments时遇到了一些问题

我有以下架构:

{
    "_id": ObjectId("58da1dd3d1f0e80cac0002e2"),
    "slug": "about-us",
    "languages": [{
            "title": "About us",
            "content": "Here is something written about us",
            "language": DBRef("languages", ObjectId("58d29616d1f0e82574007d12"), "XXX"),
            "keywords": [],
            "description": "Here you can find all information about our company"
        },
        {
            "title": "About us",
            "content": "Here is something written about us",
            "language": DBRef("languages", ObjectId("58d29616d1f0e82574007d13"), "XXX"),
            "keywords": [],
            "description": "Here you can find all information about our company"
        },
        {
            "title": "About us",
            "content": "Here is something written about us",
            "language": DBRef("languages", ObjectId("58d29616d1f0e82574007d13"), "XXX"),
            "keywords": [],
            "description": "Here you can find all information about our company"
        },
        {
            "title": "About us",
            "content": "Here is something written about us",
            "language": DBRef("languages", ObjectId("58d29616d1f0e82574007d13"), "XXX"),
            "keywords": [],
            "description": "Here you can find all information about our company"
        }
    ]
}

并创建了以下indexes

/* 1 */
{
    "key": {
        "_id": NumberInt("1")
    },
    "name": "_id_",
    "ns": "XXX.pages",
    "v": NumberInt("2")
},
/* 2 */
{
    "key": {
        "slug": NumberInt("1")
    },
    "name": "slug_1",
    "ns": "XXX.pages",
    "sparse": false,
    "unique": true,
    "v": NumberInt("2")
},
/* 3 */
{
    "key": {
        "languages.language.$id": 1
    },
    "name": "languages.language.$id_1",
    "ns": "XXX.pages",
    "sparse": true,
    "unique": true,
    "v": NumberInt("2")
}

正如您在架构中看到的,我可以在页面上创建新的EmbenddedDocuments。但它应该给我一个错误?

我使用此命令插入EmbenddedDocument

db.pages.update({
    "_id": ObjectId("58da1dd3d1f0e80cac0002e2")
}, {
    "$pushAll": {
        "languages": [{
            "title": "About us",
            "content": "Here is something written about us",
            "language": {
                "$ref": "languages",
                "$id": ObjectId("58d29616d1f0e82574007d13"),
                "$db": "XXX"
            },
            "keywords": [],
            "description": "Here you can find all information about our company"
        }]
    }
});

0 个答案:

没有答案