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

Schema.add method doesn't copy indexes #12654

Closed
2 tasks done
JorgeSilva7 opened this issue Nov 3, 2022 · 4 comments · Fixed by #12737
Closed
2 tasks done

Schema.add method doesn't copy indexes #12654

JorgeSilva7 opened this issue Nov 3, 2022 · 4 comments · Fixed by #12737
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@JorgeSilva7
Copy link

JorgeSilva7 commented Nov 3, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.6.5

Node.js version

14.18.0

MongoDB server version

4.2.21

Typescript version (if applicable)

4.8.4

Description

When i use Schema.prototype.add() method for copying schema to an other schema, the indexes aren't copied.

Steps to Reproduce

I have a test schema

const test = new Schema({
   name: String
}):

...someMethods, statics...

test.index({name: 1});

And another schema called 'extended' and i want to add test schema in extended with add() method like the documentation says "You can also add() another schema and copy over all paths, virtuals, getters, setters, indexes, methods, and statics."

const extended = new Schema({
   property: String
});

extended.add(test);

But extended schema doesn't have the index name

Expected Behavior

When i call extended.indexes() the returned array has the index {name: 1}

@hasezoey
Copy link
Collaborator

hasezoey commented Nov 8, 2022

And other schema called 'extended' and i want to add test schema in extended with add() method like the documentation say "You can also add() another schema and copy over all paths, virtuals, getters, setters, indexes, methods, and statics."

i think the documentation needs to be updated, because in the description it says Adds key path / schema type pairs to this schema. while in the code comments it says You can also 'add()' another schema and copy over all paths, virtuals, getters, setters, indexes, methods, and statics. [Schema.prototype.add()](https://mongoosejs.com/docs/api/schema.html#schema_Schema-add)


as a workaround, if test is a base-schema and you dont need to add other schemas, consider using .clone

@JorgeSilva7
Copy link
Author

But currently the add() method copy paths, methods, statics, virtuals, getters, setters. Only the indexes don't be copied.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Nov 16, 2022
@IslandRhythms
Copy link
Collaborator

I did some digging. If you define the index in the schema and then call add, if will be added correctly. If you use the index method it will not be added correctly.

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: {type: String}
});

const otherSchema = new mongoose.Schema({
  age: { type: Number}
});

otherSchema.index({ age: 1 })

testSchema.add(otherSchema);

console.log(testSchema);
console.log(testSchema.tree)
console.log(testSchema.tree.age)

Marking as a bug because if the docs say it suppose to do something, it should be doing it. Especially if the code is written so.

@JorgeSilva7
Copy link
Author

Finally i used .clone() method and .add() with .set() methods for add paths and options.

testSchema.index({...someIndex});

const otherSchema = testSchema.clone();
otherSchema.add({...paths})
otherSchema.set(someOptions)

Now otherSchema has the indexes of testSchema

@vkarpov15 vkarpov15 modified the milestones: 6.7.4, 6.7.5 Nov 26, 2022
vkarpov15 added a commit that referenced this issue Nov 30, 2022
fix(schema): copy indexes when calling `add()` with schema instance
@JorgeSilva7 JorgeSilva7 changed the title Schema.add method not copy indexes Schema.add method doesn't copy indexes Nov 30, 2022
rdeavila94 added a commit to rdeavila94/mongoose that referenced this issue Mar 6, 2023
…ticgh-12654

fix(schema): copy indexes when calling `add()` with schema instance
vkarpov15 added a commit that referenced this issue Mar 6, 2023
Merge pull request #12737 from Automattic/vkarpov15/gh-12654
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants