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

MongoServerError: $natural sort cannot be set to a value other than -1 or 1 when using $natural with another field in sorting #14588

Closed
2 tasks done
top-kat opened this issue May 13, 2024 · 3 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@top-kat
Copy link

top-kat commented May 13, 2024

Prerequisites

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

Mongoose version

8.3.4

Node.js version

18.18.2

MongoDB server version

7.0.2 Community

Typescript version (if applicable)

5.3.3

Description

I had this exact error:

MongoServerError: $natural sort cannot be set to a value other than -1 or 1.
    at Connection.sendCommand (/Users/garcias/BANGK/bangk-app copy/node_modules/mongodb/src/cmap/connection.ts:498:17)
    at async Connection.command (/Users/garcias/BANGK/bangk-app copy/node_modules/mongodb/src/cmap/connection.ts:562:22)
    at async Server.command (/Users/garcias/BANGK/bangk-app copy/node_modules/mongodb/src/sdam/server.ts:310:16)
    at async executeOperation (/Users/garcias/BANGK/bangk-app copy/node_modules/mongodb/src/operations/execute_operation.ts:181:12)
    at async FindCursor._initialize (/Users/garcias/BANGK/bangk-app copy/node_modules/mongodb/src/cursor/find_cursor.ts:78:22)
    at async FindCursor.[kInit] (/Users/garcias/BANGK/bangk-app copy/node_modules/mongodb/src/cursor/abstract_cursor.ts:644:21)
    at async next (/Users/garcias/BANGK/bangk-app copy/node_modules/mongodb/src/cursor/abstract_cursor.ts:717:7)
    at async FindCursor.[Symbol.asyncIterator] (/Users/garcias/BANGK/bangk-app copy/node_modules/mongodb/src/cursor/abstract_cursor.ts:302:26)
    at async FindCursor.toArray (/Users/garcias/BANGK/bangk-app copy/node_modules/mongodb/src/cursor/abstract_cursor.ts:438:22)
    at async mongoAfterRequest (/Users/garcias/BANGK/bangk-app copy/packages/core-backend/src/databases/mongo/mongoAfterRequest.ts:74:42)
    at async getLastOrFirst (/Users/garcias/BANGK/bangk-app copy/packages/core-backend/src/databases/mongo/mongoCreateDao.ts:270:24)
    at async getFirstN (/Users/garcias/BANGK/bangk-app copy/packages/core-backend/src/databases/mongo/mongoCreateDao.ts:63:20)
    at async /Users/garcias/BANGK/bangk-app copy/packages/core-backend/src/registerModules/registerDaoApi.ts:89:30

I did sort twice: promise.sort({ $natural: 1 }) and then promise.sort({ creationDate: 'desc' }):

When I spied the sort values in the promise (copy pasted from debugger from promise.options.sort), I had:

{
  $natural: 1,
  creationDate: -1,
}

Once I removed the creationDate sorting condition everything went fine.

My researchs

It seems that I am the only one on the internet to have had that issue since copy pasting the error code above returns NO results 🙃

Steps to Reproduce

  • Sort by natural order. Eg: promise.sort({ $natural: 1 })
  • then, sort by something else. Eg: promise.sort({ creationDate: 'desc' })

Expected Behavior

Should accept both sort conditions

@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label May 13, 2024
@IslandRhythms
Copy link
Collaborator

Please modify the following script to demonstrate your issue:

const mongoose = require('mongoose');

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

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

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

  for (let i = 0; i < 11; i++) {
    await Test.create({
      name: `Test${i}`,
      age: i
    });
  }

  const natural = await Test.find().sort({ $natural: 1 });
  console.log('This is natural', natural)
  const res = await Test.find().sort({ age: -1 });
  console.log('this is res', res);
}

run();

@vkarpov15
Copy link
Collaborator

TestModel.find().sort({ $natural: 1 }).sort({ creationDate: 'desc' }) will make the sort condition { $natural: 1, creationDate: -1 }. To make sort() overwrite the existing query sort instead of merging, use .sort({ creationDate: 'desc' }, { overwrite: true })

@top-kat
Copy link
Author

top-kat commented May 14, 2024

@vkarpov15 actually, the way it works is fine, merging is the expected behavior for me. Btw thanks for the tips ;)

@IslandRhythms thanks for your reactivity. Here is the modified script:

const mongoose = require('mongoose');

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

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

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

  for (let i = 0; i < 11; i++) {
    await Test.create({
      name: `Test${i}`,
      age: i
    });
  }

  const promise = Test.find().sort({ $natural: 1 })

  promise.sort({ age: -1 })

  console.log('this is res', await promise);
}

run();

As expected, it throws the same error:

yarn run v1.22.21
$ node ./app.js
/Users/garcias/DEV/00_test/mongoose/node_modules/mongodb/lib/cmap/connection.js:281
                    throw new error_1.MongoServerError(document);
                          ^

MongoServerError: $natural sort cannot be set to a value other than -1 or 1.
    at Connection.sendCommand (/Users/garcias/DEV/00_test/mongoose/node_modules/mongodb/lib/cmap/connection.js:281:27)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Connection.command (/Users/garcias/DEV/00_test/mongoose/node_modules/mongodb/lib/cmap/connection.js:304:26)
    at async Server.command (/Users/garcias/DEV/00_test/mongoose/node_modules/mongodb/lib/sdam/server.js:169:24)
    at async executeOperation (/Users/garcias/DEV/00_test/mongoose/node_modules/mongodb/lib/operations/execute_operation.js:126:16)
    at async FindCursor._initialize (/Users/garcias/DEV/00_test/mongoose/node_modules/mongodb/lib/cursor/find_cursor.js:55:26)
    at async [kInit] (/Users/garcias/DEV/00_test/mongoose/node_modules/mongodb/lib/cursor/abstract_cursor.js:454:27)
    at async next (/Users/garcias/DEV/00_test/mongoose/node_modules/mongodb/lib/cursor/abstract_cursor.js:514:13)
    at async [Symbol.asyncIterator] (/Users/garcias/DEV/00_test/mongoose/node_modules/mongodb/lib/cursor/abstract_cursor.js:160:34)
    at async FindCursor.toArray (/Users/garcias/DEV/00_test/mongoose/node_modules/mongodb/lib/cursor/abstract_cursor.js:273:26) {
  errorResponse: {
    ok: 0,
    errmsg: '$natural sort cannot be set to a value other than -1 or 1.',
    code: 2,
    codeName: 'BadValue'
  },
  ok: 0,
  code: 2,
  codeName: 'BadValue',
  [Symbol(errorLabels)]: Set(0) {}
}

Node.js v18.18.2
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

3 participants