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

fix(schema): support 'ascending', 'asc', 'descending', 'desc' for index direction #13761

Merged
merged 1 commit into from Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/helpers/schema/getIndexes.js
Expand Up @@ -78,7 +78,15 @@ module.exports = function getIndexes(schema) {
field[prefix + key] = 'text';
delete options.text;
} else {
const isDescendingIndex = Number(index) === -1;
let isDescendingIndex = false;
if (index === 'descending' || index === 'desc') {
isDescendingIndex = true;
} else if (index === 'ascending' || index === 'asc') {
isDescendingIndex = false;
} else {
isDescendingIndex = Number(index) === -1;
}

field[prefix + key] = isDescendingIndex ? -1 : 1;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/schema.js
Expand Up @@ -2046,6 +2046,14 @@ Schema.prototype.index = function(fields, options) {
utils.expires(options);
}

for (const field of Object.keys(fields)) {
if (fields[field] === 'ascending' || fields[field] === 'asc') {
fields[field] = 1;
} else if (fields[field] === 'descending' || fields[field] === 'desc') {
fields[field] = -1;
}
}

this._indexes.push([fields, options]);
return this;
};
Expand Down
17 changes: 15 additions & 2 deletions test/schema.test.js
Expand Up @@ -856,6 +856,21 @@ describe('schema', function() {
assert.deepEqual(indexes[1][0], { prop2: 1 });
});

it('using "ascending" and "descending" for order (gh-13725)', function() {
const testSchema = new Schema({
prop1: { type: String, index: 'ascending' },
prop2: { type: Number, index: 'descending' },
prop3: { type: String }
});
testSchema.index({ prop3: 'desc' }, { unique: true });

const indexes = testSchema.indexes();
assert.equal(indexes.length, 3);
assert.deepEqual(indexes[0][0], { prop1: 1 });
assert.deepEqual(indexes[1][0], { prop2: -1 });
assert.deepEqual(indexes[2][0], { prop3: -1 });
});

it('with single nested doc (gh-6113)', function() {
const pointSchema = new Schema({
type: {
Expand All @@ -873,8 +888,6 @@ describe('schema', function() {
assert.deepEqual(schema.indexes(), [
[{ point: '2dsphere' }, { background: true }]
]);


});

it('with embedded discriminator (gh-6485)', function() {
Expand Down
5 changes: 0 additions & 5 deletions test/types/mongo.test.ts
Expand Up @@ -2,11 +2,6 @@ import * as mongoose from 'mongoose';
import { expectType } from 'tsd';
import * as bson from 'bson';

<<<<<<< HEAD
import GridFSBucket = mongoose.mongo.GridFSBucket;

=======
>>>>>>> 6.x
function gh12537() {
const schema = new mongoose.Schema({ test: String });
const model = mongoose.model('Test', schema);
Expand Down
4 changes: 0 additions & 4 deletions types/augmentations.d.ts
Expand Up @@ -6,8 +6,4 @@ declare module 'bson' {
/** Mongoose automatically adds a conveniency "_id" getter on the base ObjectId class */
_id: this;
}
<<<<<<< HEAD
}
=======
}
>>>>>>> 6.x
4 changes: 2 additions & 2 deletions types/inferschematype.d.ts
Expand Up @@ -30,7 +30,7 @@ declare module 'mongoose' {
OptionalPaths<DocDefinition, TSchemaOptions['typeKey']>)]: ObtainDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>;
};

/**
/**
* @summary Obtains document schema type from Schema instance.
* @param {Schema} TSchema `typeof` a schema instance.
* @example
Expand All @@ -39,7 +39,7 @@ declare module 'mongoose' {
* // result
* type UserType = {userName?: string}
*/
export type InferSchemaType<TSchema> = IfAny<TSchema, any, ObtainSchemaGeneric<TSchema, 'DocType'>>;
export type InferSchemaType<TSchema> = IfAny<TSchema, any, ObtainSchemaGeneric<TSchema, 'DocType'>>;

/**
* @summary Obtains schema Generic type by using generic alias.
Expand Down