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

find by ObjectID fails #23

Open
myartsev opened this issue Sep 25, 2017 · 4 comments
Open

find by ObjectID fails #23

myartsev opened this issue Sep 25, 2017 · 4 comments

Comments

@myartsev
Copy link

myartsev commented Sep 25, 2017

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?

@gr0uch
Copy link
Member

gr0uch commented Sep 25, 2017

This handles deserialization of ObjectIDs, also need to serialize any client-side provided IDs into ObjectIDs.

@myartsev
Copy link
Author

Yes, see #22 for the serialization, I ran into this afterwards.

@gr0uch
Copy link
Member

gr0uch commented Sep 29, 2017

There needs to be a configuration option whether to use ObjectIDs globally or not. The problem is that they can not be mixed, since one must always cast strings to ObjectIDs.

@myartsev
Copy link
Author

myartsev commented Oct 2, 2017

Agreed, I tried to make it work as a mix, but for various reasons I no longer think that is a good idea / possible.

I was running out of time and I ended up switching over to using strings instead of ObjectIDs for the time being. A configuration to use Object IDs globally is a good idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants