Skip to content

find by ObjectID fails #23

@myartsev

Description

@myartsev

Related to #5; if the ID field is an ObjectID, find fails.

For example:
http://localhost:1337/foo/59c84caa76f5f2066ae21d9a
http://localhost:1337/foo/_bsontype,ObjectID,id,Y%EF%BF%BDL....

The id field is always treated as a string in the resulting query.

if (ids && ids.length) query[idKey] = { $in: ids }

There is not much to differentiate a serialized ObjectID from a normal string.

One approach would be to attempt to de-serialize the ID into an ObjectID.
If this de-serialization is successful, add the resulting object as an alternative to the lookup query.

    if (ids && ids.length) {
      ids.forEach(id => {
        var objectId
        try {
          objectId = ObjectID(id)
          ids.push(objectId)
        }
        catch(e) { /* id cannot be converted into a valid ObjectID */ }
      })

      query[idKey] = { $in: ids }
    }

This works and all variations are covered:

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions