Skip to content

Query.sort not working since 5.12.11 #10448

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
ymshenyu opened this issue Jul 13, 2021 · 2 comments
Closed

Query.sort not working since 5.12.11 #10448

ymshenyu opened this issue Jul 13, 2021 · 2 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@ymshenyu
Copy link

ymshenyu commented Jul 13, 2021

Do you want to request a feature or report a bug?
A bug (might be relevant to mongodb/node-mongodb-native#2790).
What is the current behavior?

5.12.10

[
    {
        "_id": "60dd3b60ee6dcc3702d26cba",
        "createDate": "2021-07-01T03:49:52.593Z",
    },
    {
        "_id": "60dd39cdee6dcc3702d26bd3",
        "createDate": "2021-07-01T03:43:09.566Z",
    },
]

5.12.11+

[
    {
        "_id": "60d94bc55e6194df53f70b37",
        "createDate": "2021-06-28T04:10:45.484Z",
    },
    {
        "_id": "60d94dda5e6194df53f70c18",
        "createDate": "2021-06-28T04:19:38.419Z",
    },
]

You can see mongoose return unsorted document array.

If the current behavior is a bug, please provide the steps to reproduce.

   // Model creation
   const schema = new mongoose.Schema({
             createDate: {
                       type: Date,
                       index: true
             }
   })
   const Model = mongoose.model('data', schema)
   
   // Operation
   const data = await Model.find().sort({ createDate: -1 })
   console.log(data)

What is the expected behavior?
Return sorted document array.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 16.4.2
Mongoose: 5.13.2
MongoDB: 4.2.10 Community

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

const mongoose = require('mongoose');

   // Model creation
   const schema = new mongoose.Schema({
    createDate: {
              type: Date,
              index: true
    }
})
const Model = mongoose.model('data', schema)


async function run() {
    await mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      })
    
      await mongoose.connection.dropDatabase()
// Operation
await Model.create({createDate: new Date()});
await Model.create({createDate: new Date()});
await Model.create({createDate: new Date()});
console.log('Unsorted', await Model.find());
const data = await Model.find().sort({ createDate: -1 })
console.log('Sorted', data)
}

run();

@ymshenyu
Copy link
Author

Thanks for your help. Indeed, this wasn't a bug for mongoose. Sorry for bothering you.

# for free to join this conversation on GitHub. Already have an account? # 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

2 participants