String to ObjectId automatic conversion - Mongodb
See original GitHub issueDescribe 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:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
will be fixed in v3.6.12
Here you can check this repository. I wonder how the test case is passing. Note: Use
npm start
to test.