MongoDB-为什么_id索引不对重复的条目引发错误? [英] MongoDB - Why does the _id index not throwing an error on duplicate entries?
问题描述
我对NoSQL数据库完全陌生,目前正在使用MongoDB.
I'm completely new to NoSQL databases and I'm working currently with MongoDB.
我试图理解为什么upserting a duplicate _id
文档时默认的_id
索引不引发错误.
I'm trying to understand why the default _id
index does not throw an error, when upserting a duplicate _id
document.
如文档中所述_id
默认情况下是唯一索引
As stated in the docs _id
is an unique index by default
(尽管此处未显示唯一标志.)
(although it doesn't show the unique flag here..)
> db.foo.getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.foo"
}
]
>
因此,当upserting
文档(以空集合开始)时,
如果先插入它,然后似乎忽略"它.
So when upserting
the document (started with an empty collection),
if first inserts it and then seems to "ignore" it.
> db.foo.update({ _id: 'doe123'}, { _id: 'doe123', name: 'John Doe'}, { upsert: true});
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "doe123" })
> db.foo.update({ _id: 'doe123'}, { _id: 'doe123', name: 'John Doe'}, { upsert: true});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
因此,我尝试了另一件事,并在名称"上创建了unique index
.
So I've tried another thing and created an unique index
on "name".
名称重复的结果:
> db.foo.update({ _id: 'foo456'}, { _id: 'foo456', name: 'John Doe'}, { upsert: true});
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: test.foo index: name_1 dup key: { : \"John Doe\" }"
}
})
为什么我不在重复的_id
上遇到这种错误?
Why am I not getting this kind of error on a duplicate _id
?
编辑:我正在使用MongoDB v.3.2.3
EDIT: I'm using MongoDB v.3.2.3
推荐答案
在第一种情况下,没有理由显示重复的索引错误,因为它只是尝试更新同一记录的_id
和name
字段具有相同的值.
There is no reason to show duplicate index error in the first case as it is just trying to update the _id
and name
fields of the same record with the same value.
如果可以尝试
db.foo.update({ _id: '1098'}, { _id: 'doe123', name: 'John Doe'}, { upsert: true});
您将收到错误消息,因为查询正尝试使用具有某些_id
值的其他_id
更新记录.
you will get error, as query is trying to update record with different _id
with some existing _id
value.
在第二种情况下,您首先使用name
字段创建了一条记录,然后尝试在另一条记录中更新相同的名称,由于name
是唯一索引,因此会出现错误.
In second case, you created a record first with name
field and then you are trying update the same name in another record, which will give error as name
is unique index.
-
如果您正在尝试
db.foo.insert({ _id: 'doe123', name: 'John Doe'});
会给您错误,因为在这种情况下,您试图插入一条已经存在的记录,即_id
是唯一的,并且您试图再创建一个具有相同_id
值的记录.
will give you the error, as in this case you are trying to insert a record which is already present i.e. _id
is unique and you are trying to create one more record with same _id
value.
upsert:true
:-如果upsert为true,并且没有文档符合查询条件,则update()将插入单个文档.
upsert:true
:- if upsert is true and no document matches the query criteria, update() inserts a single document.
如果upsert为true,并且存在符合查询条件的文档,则update()将执行更新.
If upsert is true and there are documents that match the query criteria, update() performs an update.
这篇关于MongoDB-为什么_id索引不对重复的条目引发错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!