williamkapke / mongo-mock

Let's pretend we have a real MongoDB
MIT License
240 stars 74 forks source link

Mixed projections types not allowed #120

Closed purefan closed 4 years ago

purefan commented 4 years ago

Hey guys!

Loving the project, it allows me to really test my express app from a mocha testsuite but today I ran into an issue when I was refactoring, this query:

db.getCollection('position').findOne({
            status: 0
        }, {
            sort: [ [ 'priority', 'desc' ] ]
        })

works against a normal mongo database, but in mongo-mock it fails with:

mongo-mock:collection findOne {"query":{"status":0},"fields":{"sort":[["priority","desc"]]},"skip":0,"limit":0} callback=undefined +0ms mongo-mock:cursor initializing cursor +0ms Error: Mixed projections types not allowed

Is it something Im doing wrong?

williamkapke commented 4 years ago

The 2nd parameter is a projection (an object that says which fields to return). Your query's 2nd parameter tries to sort ... mongo-mock is getting confused with it.

https://docs.mongodb.com/manual/reference/method/db.collection.findOne/

I suspect real mongo isn't sorting and is just ignoring it..? not sure.

purefan commented 4 years ago

I changed the way I was querying based on your feedback and it works now, I havent confirmed that mongo wasnt sorting but I expected an error from mongo, it just never came and I thought my code was fine.

Thanks for your feedback! loving the project <3