1

I have this relationship (not nested documents, an actual relationship with them by id) (the following is a GraphQL representation)

  appointment(id:"5aefada83ed11a4862ae908a") {
    _id
    location {
      _id
      name
      practice {
        _id
        name
      }
    }
  }

I was wondering what is the syntax for querying the appointment by practice. I have tried the following but no luck

return Appointment
.findOne(_.pick(params, ["_id", "location.practice._id"]))
.populate(_.keys(_.groupBy(_.reject(strapi.models.appointment.associations, {autoPopulate: false}), 'alias')).join(' '));

Where params is

{ _id: '5aefada83ed11a4862ae908a',
  'location.practice._id': 5aef661dca53d04354a36adc }

According to the nested documents docs, the dot syntax should work. Doing the same in a where clause doesn't seem to work either. Any ideas?

Update: Include the full example of the request (without trying to filter it by practice)

The document with its nested relations

JSON of the appointment (using Robo 3T)

{
    "_id" : ObjectId("5aefada83ed11a4862ae908a"),
    "starts_at" : ISODate("2018-05-02T00:00:00.000Z"),
    "ends_at" : ISODate("2018-05-01T00:00:00.000Z"),
    "createdAt" : ISODate("2018-05-07T01:36:40.685Z"),
    "updatedAt" : ISODate("2018-05-07T01:36:54.780Z"),
    "__v" : 0,
    "id" : ObjectId("5aefada83ed11a4862ae908a"),
    "location" : ObjectId("5af52263900e1c1b1cc2374f"),
    "patient" : ObjectId("5aee7ee027d7e03b2e0c7c3b"),
    "doctor" : ObjectId("5aee1e6b7ae7d334d9247e79")
}

JSON of the location (using Robo 3T)

{
    "_id" : ObjectId("5af52263900e1c1b1cc2374f"),
    "name" : "Ottawa",
    "createdAt" : ISODate("2018-05-11T04:56:03.024Z"),
    "updatedAt" : ISODate("2018-05-11T04:56:57.932Z"),
    "__v" : 0,
    "id" : ObjectId("5af52263900e1c1b1cc2374f"),
    "practice" : ObjectId("5aef661dca53d04354a36adc")
}

JSON of the practice (using Robo 3T)

{
    "_id" : ObjectId("5aef661dca53d04354a36adc"),
    "name" : "Demo",
    "email" : "demo@odonto.me",
    "createdAt" : ISODate("2018-05-06T20:31:25.456Z"),
    "updatedAt" : ISODate("2018-05-06T20:31:25.469Z"),
    "__v" : 0,
    "id" : "5aef661dca53d04354a36adc"
}

The queries happening on the background when querying this model

Mongoose: users-permissions_user.findOne({ _id: ObjectId("5aee1e6b7ae7d334d9247e79") }, { fields: {} })
Mongoose: users-permissions_role.find({ _id: { '$in': [ ObjectId("5aedd8b3e235fa32d54c7b63") ] } }, { fields: {} })
Mongoose: appointment.find({ staff: { '$in': [ ObjectId("5aee1e6b7ae7d334d9247e79") ] } }, { fields: {} })
Mongoose: practice.find({ _id: { '$in': [ ObjectId("5aef661dca53d04354a36adc") ] } }, { fields: {} })
Mongoose: users-permissions_permission.findOne({ role: ObjectId("5aedd8b3e235fa32d54c7b63"), type: 'application', controller: 'appointment', action: 'findone', enabled: true }, { fields: {} })
Mongoose: appointment.findOne({ _id: ObjectId("5aefada83ed11a4862ae908a") }, { fields: {} })
Mongoose: patient.find({ _id: { '$in': [ ObjectId("5aee7ee027d7e03b2e0c7c3b") ] } }, { fields: {} })
Mongoose: location.find({ _id: { '$in': [ ObjectId("5af52263900e1c1b1cc2374f") ] } }, { fields: {} })
Mongoose: location.findOne({ _id: ObjectId("5af52263900e1c1b1cc2374f") }, { fields: {} })
Mongoose: practice.findOne({ _id: ObjectId("5aef661dca53d04354a36adc") }, { fields: {} })
[2018-05-14T01:51:09.117Z] debug POST /graphql?_id=5aefada83ed11a4862ae908a (32 ms)
raulriera
  • 714
  • 10
  • 28
  • How did you solve this eventually ? I recognize Strapi embedded components, isn't it ? I ran into the same question after realizing these components are not subdocuments neither relationships, and I can't find the way to query collection by component.field... Thanks a lot – Kojo Nov 12 '20 at 15:13

1 Answers1

0

What you are trying to achieve is right. The only thing you are lagging at is _.pick method. The output of this:

_.pick(params, ["_id", "location.practice._id"]) 

would be

_id: 'your value',
location: { practice: { _id: 'some value' } } }

But what you need is this:

_id: 'your value',
'location.practice._id': 'some value'

Hope this helps

  • Hi, the response from the _.pick method is `{ _id: '5aefada83ed11a4862ae908a', 'location.practice._id': '5aef661dca53d04354a36adc' }` which appears to be what you suggested. This will return nothing from the database, but if I remove the ` 'location.practice._id': '5aef661dca53d04354a36adc'` part, it does work... (and that is the correct id for the practice) :( – raulriera May 13 '18 at 16:59
  • 1
    this is because your response from _.pick method would be _id: 'your value', location: { practice: { _id: 'some value' } } }, which is composed of a nested object. Thus mongo db does not supports it. I'll say if you want to debug it correctly add mongoose.set("debug", true) after setting a connection with mongodb vai mongoose. This would print the exact query in your console which is being queried to mongodb via mongoose – Parth Mahajan May 13 '18 at 17:09
  • I don't get it, the response from pick is not a nested object, but the value pasted in the previous comment and in the original question. I'll turn on debugging tho, that would be extremely helpful – raulriera May 13 '18 at 17:18
  • Ummm I was doubtful there too that's why I ran my own set of test and the result it showed me was a nested object. I might be wrong if you are getting what you expect. Anyway, Do let me know if debugging it would solve you problem. – Parth Mahajan May 13 '18 at 17:21
  • The debug option is great! TIL... this is the query actually being performed `Mongoose: appointment.findOne({ _id: ObjectId("5aefada83ed11a4862ae908a"), 'location.practice._id': ObjectId("5aef661dca53d04354a36adc") }, { fields: {} })` – raulriera May 13 '18 at 17:28
  • Ohh. Mongoose is not converting 5aef661dca53d04354a36adc ( In 'location.practice._id': '5aef661dca53d04354a36adc' ) to mongoose object Id. Try running your query like this : 'location.practice._id': mongoose.Types.ObjectId('5aef661dca53d04354a36adc'). Also, if you want to automate it for then you can do this. result_of_pick = _.pick(Your arguments); for(let key in result_of_pick) if !(result_of_pick[key] instance of mongoose.Types.ObjectId ) result_of_pick[key] = mongoose.Types.ObjectId(result_of_pick[key]) – Parth Mahajan May 13 '18 at 17:38
  • Thanks, made a mistake using the wrong field, updated the response to use the proper ObjectId.. still no luck `appointment.findOne({ _id: ObjectId("5aefada83ed11a4862ae908a"), 'location.practice': ObjectId("5aef661dca53d04354a36adc") }, { fields: {} })` still returns nothing – raulriera May 13 '18 at 18:47
  • Can you post any one of your mongodb document for appointment ? – Parth Mahajan May 13 '18 at 18:49
  • Sure, updated the question with the full object and all the queries happening in the background... thanks for all your help – raulriera May 13 '18 at 19:00
  • @raulriera you missed _id in ‘ location.practice ‘ here. That’s a typo ? – Parth Mahajan May 13 '18 at 19:29
  • Oh yeah, I was playing around with different values and must have pasted one of those...it's a typo, a few comments back is correct – raulriera May 13 '18 at 19:55