查询Mongoosejs中的嵌套关​​系

时间:2018-05-13 15:27:55

标签: node.js mongodb mongoose strapi

我有这种关系(不是嵌套文档,通过id与它们建立实际关系)(以下是GraphQL表示)

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

我想知道练习查询约会的语法是什么。我试过以下但没有运气

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条款中执行相同操作似乎也无效。 有什么想法吗?

更新:包含请求的完整示例(不尝试按惯例过滤)

具有嵌套关系的文档

约会的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 个答案:

没有答案