【问题标题】:MongoDB aggregation (via MongoEngine/PyMongo): can I access ReferenceField objects?MongoDB 聚合(通过 MongoEngine/PyMongo):我可以访问 ReferenceField 对象吗?
【发布时间】:2016-04-29 23:46:11
【问题描述】:

假设我有两个这样的模型类:

class Institution(Model):
    name = StringField()

class Student(Model):
    full_name = StringField()
    institution = ReferenceField(Institution)

是否可以在聚合管道中访问Student.institution.name?

【问题讨论】:

  • 你能显示你的查询吗?如果您要问的是如何从Student 对象获取name,那么是的!你可以做s.institution.name

标签: mongodb pymongo mongoengine


【解决方案1】:

从 monogdb 版本 3.2 可以使用 $lookup,但有一些限制(例如,不适用于分片集合),例如:

db.student.aggregate([
    {
      $lookup:
        {
          from: "institution",
          localField: "institution",
          foreignField: "_id",
          as: "institution"
        }
   }
])

【讨论】:

    猜你喜欢
    • 2014-08-16
    • 2012-02-29
    • 1970-01-01
    • 2013-03-07
    • 2020-02-26
    • 2019-10-31
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多