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

Index not created when using ascending or descending #13725

Closed
2 tasks done
Vedant-Gandhi opened this issue Aug 12, 2023 · 2 comments · Fixed by #13761
Closed
2 tasks done

Index not created when using ascending or descending #13725

Vedant-Gandhi opened this issue Aug 12, 2023 · 2 comments · Fixed by #13761
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Vedant-Gandhi
Copy link

Prerequisites

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

Mongoose version

7.4.1

Node.js version

18.6.1

MongoDB server version

6.0.8

Typescript version (if applicable)

5.1.6

Description

I have recently observed an issue when creating index on mongoose Schema using the index() method.When I assign the value to a field as ascending or descending as recommended by Typescript Intellisense the index is not created in the database.

When I replace it with numerical representations such as 1 or -1 it does work as expected and the index is created.

Steps to Reproduce

  1. Create a raw schema for any type. Use mongoose with Typescript.
  2. Create an index using the index method on a single field. eg - orderSchema.index({ createdAt: "ascending" }, { unique: true })
  3. Create the model and run the code.
  4. Check the mongodb to verify if the index has been created or not.

Expected Behavior

The index should be created according to specified value. If ascending is specified it must be created in ascending order and vice versa.

@vkarpov15 vkarpov15 added this to the 7.4.4 milestone Aug 14, 2023
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Aug 14, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Aug 16, 2023
@IslandRhythms
Copy link
Collaborator

For me its not creating the index because its throwing an error.

const mongoose = require('mongoose');

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

testSchema.index({ createdAt: "ascending" }, { unique: true });

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017/gh13725');
  await mongoose.connection.dropDatabase();

  await Test.init();
  await Test.create({
    name: 'Test Testerson'
  });
}

run();
$ node ./13725.js
C:\Users\Drumm\Desktop\Work\debugging\JavaScript\node_modules\mongodb\lib\cmap\connection.js:202
                callback(new error_1.MongoServerError(document));
                         ^

MongoServerError: Error in specification { unique: true, background: true, name: "createdAt_ascending", key: { createdAt: "ascending" } } :: caused by :: Unknown index plugin 'ascending'
    at Connection.onMessage (C:\Users\Drumm\Desktop\Work\debugging\JavaScript\node_modules\mongodb\lib\cmap\connection.js:202:26)
    at MessageStream.<anonymous> (C:\Users\Drumm\Desktop\Work\debugging\JavaScript\node_modules\mongodb\lib\cmap\connection.js:61:60)
    at MessageStream.emit (node:events:526:28)
    at processIncomingData (C:\Users\Drumm\Desktop\Work\debugging\JavaScript\node_modules\mongodb\lib\cmap\message_stream.js:124:16)
    at MessageStream._write (C:\Users\Drumm\Desktop\Work\debugging\JavaScript\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:389:12)
    at _write (node:internal/streams/writable:330:10)
    at MessageStream.Writable.write (node:internal/streams/writable:334:10)
    at Socket.ondata (node:internal/streams/readable:754:22)
    at Socket.emit (node:events:526:28) {
  ok: 0,
  code: 67,
  codeName: 'CannotCreateIndex',
  [Symbol(errorLabels)]: Set(0) {}
}

@Vedant-Gandhi
Copy link
Author

If we do not call the Test.init() function it does not throw any kind of error. In the documentation it says as follows -
Mongoose calls this function automatically when a model is created using mongoose.model() or connection.model(), so you don't need to call init() to trigger index builds.

When we add Test.init() it throws the error but the type system still recommends the ascending,asc,etc. options.

Try running following code :-

import mongoose from "mongoose"

const url = "mongodb://127.0.0.1:27017/mydb"

const mySchema = new mongoose.Schema({
    name: { type: String },
})

mySchema.index({ name: "ascending" })

const model = mongoose.model("myschema", mySchema, "myschema")

async function runMyCode() {
    await mongoose.connect(url)
    await mongoose.connection.dropDatabase()

    await model.create({ name: "Test_User" })
}
runMyCode()

vkarpov15 added a commit that referenced this issue Aug 22, 2023
fix(schema): support 'ascending', 'asc', 'descending', 'desc' for index direction
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
3 participants