admin管理员组文章数量:1391943
if my schema looks like this:
const schema = new mongoose.Schema({
username: {
type: String,
},
password: String,
});
and I want to add "unique: true" to the schema. the relevant unique index is only created if there are no duplicates for the username field in the collection. so do I have to make sure that there are no duplicates before adding "unique: true" to the schema? or is there a way to do it even if there are duplicates so it works with future inputs?
if my schema looks like this:
const schema = new mongoose.Schema({
username: {
type: String,
},
password: String,
});
and I want to add "unique: true" to the schema. the relevant unique index is only created if there are no duplicates for the username field in the collection. so do I have to make sure that there are no duplicates before adding "unique: true" to the schema? or is there a way to do it even if there are duplicates so it works with future inputs?
Share Improve this question asked Mar 12 at 15:47 لوسيفر جبريللوسيفر جبريل 251 silver badge3 bronze badges 4- 1 Yes, you will need to check for duplicates first. Otherwise, the unique index creation will fail. From the docs: MongoDB cannot create a unique index on the specified index field(s) if the collection already contains data that would violate the unique constraint for the index. – aneroid Commented Mar 12 at 15:50
- What do you mean when you say "so it works with future inputs"? Are you referring to after the index is created or something else? – user20042973 Commented Mar 12 at 17:14
- @aneroid Thanks a lot. This answers my question. I checked the following part of the documentation but failed to look farther before asking the question. mongodb/docs/compass/current/indexes – لوسيفر جبريل Commented Mar 12 at 17:34
- @user20042973 by my question I meant if I have in my collection two documents with duplicate values, can I start adding new documents that do not accept duplicate values so the duplication is limited to the first two documents while any other document that gets created after that can't have duplicates? but I understand now that this can't be the case. thank you. – لوسيفر جبريل Commented Mar 12 at 17:36
1 Answer
Reset to default 3@aneroid's comment, copied here for posterity, answers part of the concern:
Yes, you will need to check for duplicates first. Otherwise, the unique index creation will fail. From the docs: MongoDB cannot create a unique index on the specified index field(s) if the collection already contains data that would violate the unique constraint for the index.
There may be something to add with regards to this latter question:
or is there a way to do it even if there are duplicates so it works with future inputs?
Your question specifically asks about specifying uniqueness for the Mongoose schema itself and, as you mentioned, Mongoose utilizes a unique
index in the database to provide this constraint. If you were to configure this directly in the database first, then this capability as of version 6.0
is relevant:
Starting in MongoDB
6.0
, you can use theprepareUnique
andunique
options for thecollMod
command to convert an existing standard index to a unique index.
And from the collMod
command page (emphasis added):
prepareUnique: <boolean>, // Reject new duplicate index entries
...
prepareUnique
: A boolean that determines whether the index will accept new duplicate entries.New duplicate entries fail with DuplicateKey errors when
prepareUnique
istrue
. The resulting index can be converted to a unique index. To convert the index, usecollMod
with theunique
option.If an existing index is updated so that
prepareUnique
istrue
, the index is not checked for pre-existing, duplicate index entries.
A full tutorial of this process seems to be available on the Convert an Existing Index to a Unique Index page.
本文标签:
版权声明:本文标题:node.js - how can the index for a field in a document in a mongo database be updated to reflect the changes I made in the mongoo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744743093a2622725.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论