在Mongoosejs中查询嵌套关系

10 浏览
0 Comments

在Mongoosejs中查询嵌套关系

我有一个关联(不是嵌套文档,而是通过id实际关联的)(以下为GraphQL表示):

appointment(id:"5aefada83ed11a4862ae908a") {

_id

location {

_id

name

practice {

_id

name

}

}

}

我想知道如何通过practice查询appointment的语法。我尝试了以下方法,但没有成功:

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

其中params为:

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

根据嵌套文档的文档,点语法应该起作用。在where子句中进行相同的操作似乎也不起作用。

有什么想法吗?

更新:包括请求的完整示例(不尝试通过practice进行过滤)

带有嵌套关系的文档

预约的JSON(使用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(使用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(使用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"
}

查询此模型时发生的后台查询

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)

0