question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

String to ObjectId automatic conversion - Mongodb

See original GitHub issue

Describe the bug I use mikro-orm as part of an event storage system, sometimes an event sends a Payload containing an ObjectId like a string from another system. Mikro-orm automatically turns it into an ObjectId. This is a behavior that forces us to adapt our serialization.

To Reproduce I use:

@Entity()
export class DataContainer extends BaseEntity {

  constructor(data: any) {
    super();
    this.data = data;
  }

  @Property()
  data: any; // typing used to have some edge cases
}

The test:

test('do not automatically convert string to ObjectId in the all cases', async () => {
    const dataContainer = new DataContainer({
      partnerIdFromAnotherSystem: '0000007b5c9c61c332380f78',
    });

    expect(dataContainer.data.partnerIdFromAnotherSystem).toBe('0000007b5c9c61c332380f78');

    await orm.em.persistAndFlush(dataContainer);

    expect(dataContainer.data.partnerIdFromAnotherSystem).not.toBeInstanceOf(ObjectId);

    // need to clear to lost the original typing 'string'
    orm.em.clear();

    const getDataContainer = await orm.em.findOne(DataContainer, { id: dataContainer.id });
    expect(getDataContainer!.data.partnerIdFromAnotherSystem).not.toBeInstanceOf(ObjectId);
});

Expected behavior I don’t know what would be the best solution for this case. But it is an implicit behavior that cannot be customized.

Versions

Dependency Version
node 12.13.0
typescript 3.6.5
mikro-orm 3.4.1
your-driver mongodb

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
B4nancommented, Apr 26, 2020

will be fixed in v3.6.12

1reaction
iamomiidcommented, Apr 26, 2020

Here you can check this repository. I wonder how the test case is passing. Note: Use npm start to test.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert string to ObjectID in MongoDB - Stack Overflow
I am developing an API using Codeigniter and MongoDB ...
Read more >
How to convert a String field to ObjectId in MongoSinkConnector
What I want to do is to make the sink connector treat the userId attribute as the _id (of type ObjectId) in the...
Read more >
Convert string to objectid in MongoDB? - Tutorialspoint
To convert string to objectid in MongoDB, use $toObjectId. Let us create a collection with documents − > db.demo95.
Read more >
[C# Driver] ObjectId <-> string implicit conversion
It would be nice if ObjectId could be implicitly converted to / from a string. An example is when using a SignalR hub...
Read more >
3 Ways to Convert a String to a Date in MongoDB
If you have a MongoDB collection with dates stored as strings, you can convert those into the Date BSON type if required.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found