Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to modify indexes before #10446

Closed
chumager opened this issue Jul 12, 2021 · 3 comments
Closed

Allow to modify indexes before #10446

chumager opened this issue Jul 12, 2021 · 3 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@chumager
Copy link

Do you want to request a feature or report a bug?
Feature
What is the current behavior?
Hi, I've two problems about indexes, the first one is I use soft delete, so if I've a unique index, I need to add an partialFilterExpression to avoid problems.

Now I've a plugin to spread data to make partitions, so my plugin adds a _zone field, but in this plugin I need to change all the indexes generated in the schemas, I've no problem duplicating the indexes but my problem is the unique indexes, because now the unique index must include the _zone field.

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?
I need to be able to modify the indexes generated in the schema, so the plugin can add the field to every index.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node: v16.4.1
mongodb: 4.4.6
mongoose: 5.13.2

@IslandRhythms IslandRhythms added the new feature This change adds new functionality, like a new method or class label Jul 12, 2021
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed new feature This change adds new functionality, like a new method or class labels Jul 13, 2021
@vkarpov15 vkarpov15 modified the milestones: 5.13.3, 5.13.4 Jul 13, 2021
@vkarpov15 vkarpov15 modified the milestones: 5.13.4, 5.13.5, 5.13.6 Jul 28, 2021
@vkarpov15
Copy link
Collaborator

You can modify schema indexes in place by looping over schema.indexes() and modifying each index as shown below.

'use strict';
  
const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  await mongoose.connection.dropDatabase();

  const schema = new Schema({ email: String, isDeleted: { type: Boolean, default: false } });
  schema.index({ email: 1 }, { unique: true, partialFilterExpression: { isDeleted: false } });

  schema.plugin(schema => {
    schema.add({ _zone: String });
    // Loop over every index in the schema
    for (const index of schema.indexes()) {
      if (index._zone != null) {
        continue;
      }
      // Add `_zone` as the first key in the index
      index[0] = { _zone: 1, ...index[0] };
    }
  });

  console.log(schema.indexes());
}

@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Aug 9, 2021
@chumager
Copy link
Author

Cool!!!

Thanks a lot.

I owe you a beer
Best Regards.

@vkarpov15
Copy link
Collaborator

Happy to help 🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

3 participants