Skip to content

ObjectId constructor does not accept a string #2904

Closed

Description

@swapzone

I ran into problems when trying to create a new ObjectId Object with a given String.
There is no constructor function given in your exported ObjectId class.

Could you add this constructor in:
export class ObjectId {}

Thank you, Sascha

Activity

  1. Igorbek commented on Oct 1, 2014

    @Igorbek
    Collaborator

    What library do you mean?

  2. swapzone commented on Oct 1, 2014

    @swapzone
    Author

    The mongoose library.

    On Wed, Oct 1, 2014 at 5:52 PM, Igor Oleinikov [email protected]
    wrote:

    What library do you mean?

    —
    Reply to this email directly or view it on GitHub
    #2904 (comment)
    .

  3. Igorbek commented on Oct 1, 2014

    @Igorbek
    Collaborator

    Can't find in docs/sources/tests/samples any use case where the constructor is used with parameters.
    Can you provide any from official source (docs or their repo)?

  4. swapzone commented on Oct 1, 2014

    @swapzone
    Author

    I can only describe my personal use case and refer to StackOverflow:
    http://stackoverflow.com/questions/6578178/node-js-mongoose-js-string-to-objectid-function

    On 01.10.2014, at 21:45, Igor Oleinikov [email protected] wrote:

    Can't find in docs/sources/tests/samples any use case where the constructor
    is used with parameters.
    Can you provide any from official source (docs or their repo)?

    —
    Reply to this email directly or view it on GitHub
    #2904 (comment)
    .

  5. Igorbek commented on Oct 1, 2014

    @Igorbek
    Collaborator

    It uses driver-specific constructor. There is another answer, which more situable:

    mongoose.mongo.BSONPure.ObjectID.fromString("4eb6e7e7e9b7f4194e000001");

    In case of native driver ObjectID is mapped to BSONPure.ObjectID but it's a generic way.

  6. evereq commented on Mar 4, 2015

    @evereq

    Well, fromString will not exists anymore (see Automattic/mongoose#1890), so instead of using mongoose.mongo.BSONPure.ObjectID.fromString correct way probably will be to just do following:
    var objectId = new mongoose.Types.ObjectId("4eb6e7e7e9b7f4194e000001");

    So, in the mongoose.d.ts should be constructor with string parameter, e.g:

     export module Types {
        export class ObjectId {
            toHexString(): string;
            constructor(id:string);
        }
     }
    

    Please let me know if you want me to do Pull request for this or you can do this change yourself.
    Thanks!

  7. Agamnentzar commented on May 18, 2015

    @Agamnentzar
    Contributor

    Fixed it here: #4373

  8. added a commit that references this issue on Jan 22, 2026
    c6757a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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