Skip to content

Waterline's sails-mongo adapter fails to convert query input to ObjectId when applying certain operators #6979

Open
balderdashy/sails-mongo
#484
@Rua-Yuki

Description

@Rua-Yuki

Node version: v8.17.0
Sails version (sails): 1.2.3
ORM hook version (sails-hook-orm): 2.1.1
DB adapter & version: [email protected]

No additional top-level dependencies present. Confirmed with merely sails, sails-hook-orm and sails-mongo.


There appears to be an issue where the sails-mongo Waterline adapter fails to transform input query filter values to Mongo ObjectId instances where appropriate, instead leaving such values as simple strings.

This occurs in cases where the target field appears to be an appropriate candidate (a primary key) for normalisation to ObjectId and only affects the following operators:

  • <
  • <=
  • >
  • >=

For instance, the following Waterline query:

Post.find({
  id: {
    '>': '5e9a49923a48025cccdaee43',
  },
});

Will ultimately issue this to MongoDB:

{
  _id: {
    '$gt': '5e9a49923a48025cccdaee43'
  }
}

Rather than the expected query of:

{
  _id: {
    '$gt': ObjectId('5e9a49923a48025cccdaee43')
  }
}

This results in an inability to perform a number of useful queries against PK fields (such as paging on document IDs) and feels inconsistent with the behaviour of the other Waterline query operators supported by sails-mongo, as well as other adapters.

I've built a mostly minimal test case at Rua-Yuki/waterline-mongo-objectid-comparison-funk, which also contains more information about this issue.

A quick fix exists in the fpm-git/sails-mongo fork.

Metadata

Metadata

Assignees

No one assigned

    Labels

    has prThere is an open pull request (in this repo or elsewhere) related to this issue.mongoIssue only occurs when using MongoDBormRelated to models, datastores, orm config, Waterline, sails-hook-orm, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions