I have a model with a number of properties. One of those is a field named "developerId" that contains a string which has the same length and characteristics as a MongoDB ObjectId (it is in fact a stringified ObjectId).
When I query the model with the find() method in Node.js the query is updated before it is executed and the "developerId" value is converted to an ObjectId which then fails to match any strings in the database because they are strings, not ObjectIds.
{where: {developerId: '55118c5fc921fa170f05080b'}}
Is converted to:
{where: {developerId: ObjectId('55118c5fc921fa170f05080b')}}
The field is not an id field, is specified as a type: 'string' in the model json definition.
How do I switch off this auto-object-id behaviour so that I have control over Loopback's queries?
Looks like you've uncovered a bug/shortcoming of the framework. See here:
https://github.com/strongloop/loopback-connector-mongodb/issues/52
The bug seems to still be unresolved as of two months ago. Welcome to the wild west that can be node development.
You could fork and hack the module in the short-term, while working with the community to resolve this issue.
You might also try to use the underlying mongo connection to make the query and then to map that back to your loopback objects. You can get that like so:
app.models.User.dataSource.connector
I suppose you can always change the developerId field your model to be an actual ObjectId.
You can now set strictObjectIDCoercion
flag to true
in model definition json file, to avoid coercion of id-like strings to ObjectID types.
Docs: https://github.com/strongloop/loopback-connector-mongodb#strictobjectidcoercion-flag
Here is the example from the docs:
{
"name": "myModelName",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true,
"strictObjectIDCoercion": true
},
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With