最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

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 the prepareUnique and unique options for the collMod 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 is true. The resulting index can be converted to a unique index. To convert the index, use collMod with the unique option.

If an existing index is updated so that prepareUnique is true, 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论