Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.find() and .delete() issues on a DefaultHasManyRepository with MongoDB connector #1987

Closed
loredanacirstea opened this issue Nov 6, 2018 · 4 comments
Assignees
Labels
db:MongoDB Topics specific to MongoDB Relations Model relations (has many, etc.)

Comments

@loredanacirstea
Copy link

Description / Steps to reproduce / Feature proposal

Using loopback-connector-mongodb and a hasMany relationship between two Mongo collections (PipeContainer and PipeFunction).

Inside my PipeContainerController, I can successfully do await this.pipeContainerRepository.functions(containerid).create(pipeFunctionDataObject); and it correctly creates the new PipeFunction record.

However, the following do not work:

await this.pipeContainerRepository.functions(containerid).find();
await this.pipeContainerRepository.functions(containerid).delete();

Current Behavior

await this.pipeContainerRepository.functions(containerid).find();

returns [] even though there are records to be returned.
I also tried find({where: {_id: "some_valid_id"}}) and again, it returned [].

await this.pipeContainerRepository.functions(containerid).delete();

Does not remove any if the PipeFunctions linked to PipeContainer through containerid

I assume the problem is with where.

Expected Behavior

.find(); on a DefaultHasManyRepository should have returned a non empty array.
.delete(); should have removed all the PipeFunction records with the given containerid.

@loredanacirstea
Copy link
Author

My containeridfield is a string. I am using MongoDB, so in fact it is an ObjectId(<string>)).
I assume the above issue is related to the fact that when querying:

  • where = {containerid: "5bdf94e84388069b10351f23"} returns []
  • where = {containerid: {like: "5bdf94e84388069b10351f23"}} returns all the pipefunctions with containerid == "5bdf94e84388069b10351f23"

console.log(this.pipeContainerRepository.functions("5bdf94e84388069b10351f23")) gives:

DefaultHasManyRepository {
  getTargetRepository: [Function: getter],
  constraint: { containerid: '5bdf94e84388069b10351f23' } }

Note the constraint.

@b-admike b-admike self-assigned this Nov 7, 2018
@jormar
Copy link

jormar commented Nov 14, 2018

I have the same problem here. I tried where = {containerid: {like: "5bdf94e84388069b10351f23"}} and it works... but, it doesn't seem like right.

@b-admike
Copy link
Contributor

Hi @loredanacirstea @jormar, sorry for the late reply and thank you for the detailed reproduction steps. I was able to see the issue and I think for this to work, we would need to set the strictObjectIDCoercion flag (see https://github.com/strongloop/loopback-connector-mongodb#strictobjectidcoercion-flag) on the model that is being queried, in this case, PipeFunction, so that the id is not coerced into ObjectID. To do this, you'd need to add it to the settings of the model by supplying the metadata in the model decorator for the target model as follows:

@model({
  settings: {
    strictObjectIDCoercion: true,
  },
})

@b-admike
Copy link
Contributor

Closing this as resolved, but feel free to re-open @loredanacirstea @jormar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db:MongoDB Topics specific to MongoDB Relations Model relations (has many, etc.)
Projects
None yet
Development

No branches or pull requests

4 participants