Loopback MongoDB String Property Converted to ObjectId When Using Find Where Clause
Asked Answered
A

3

10

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?

Alveolate answered 4/7, 2015 at 11:37 Comment(0)
A
4

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.

Allveta answered 4/7, 2015 at 11:56 Comment(0)
H
5

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
  },
...
}
Holbein answered 6/4, 2018 at 18:42 Comment(0)
A
4

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.

Allveta answered 4/7, 2015 at 11:56 Comment(0)
H
0

make sure in your Json file developerId properties is defined as object sample:

"Properties" {
...
    "developerId": {
      "type": {
        "required": true
      }
    }
...
}
Hurdygurdy answered 8/1, 2017 at 0:38 Comment(1)
Thanks for answering but I don't think you understand the problem. The field has already been defined in the model. The issue is about auto-conversion from string to ObjectId which in this case is unwanted.Alveolate

© 2022 - 2024 — McMap. All rights reserved.