Skip to content

_objectifyId / ObjectID.isValid converts legit string IDs to Mongo ObjectId #92

@jscottsf

Description

@jscottsf

Steps to reproduce

  1. Assume a Mongo collection that has documents with _id fields of type string rather than ObjectId.
  2. Now try to get() a document with a string _id that ObjectID.isValid considers to be a valid ObjectId. E.g. service.get('my-string-id'). The call fails to find the document since the string "my-string-id" is cast to an ObjectId.

Actual behavior

The get() call fails since _objectifyId blindly casts a passed _id. Since the documents are stored with string _id values, the call fails to find the documents.

Expected behavior

The decision to cast a string _id to ObjectId or leave it as string should be configurable in the service options. The service allows you to configure the name of the "id" field, but not whether it should be casted. Note that the field MUST be named "_id" for Mongo to consider it a primary key anyway.

In short, it's possible to have a field named "_id" and have it be of the type string. _objectifyId should not cast it if you want it to remain as string.

Note

From the MongoDB manual: The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of ANY TYPE other than an array.

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