-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Comments
i think the documentation needs to be updated, because in the description it says as a workaround, if |
But currently the |
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. |
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 |
fix(schema): copy indexes when calling `add()` with schema instance
…ticgh-12654 fix(schema): copy indexes when calling `add()` with schema instance
Prerequisites
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
schemaAnd another schema called 'extended' and i want to add
test
schema inextended
with add() method like the documentation says "You can alsoadd()
another schema and copy over all paths, virtuals, getters, setters, indexes, methods, and statics."But extended schema doesn't have the index
name
Expected Behavior
When i call
extended.indexes()
the returned array has the index{name: 1}
The text was updated successfully, but these errors were encountered: