我有集合userdomain,我想要一个结合它们的视图.

db.createView( "userDomainRelation", "user", [
   {
      $lookup:
         {
            from: "domain",
            localField: "domain",
            foreignField: "_id",
            as: "matchDomain"
         }
   },
   {
      $project:
         {
           _id: 1,
           email: 1,
           firstName: 1,
           lastName: 1,
           accountType: 1,
           domain: 1,
           host: "$matchDomain.host",
           isOpenDomain: "$matchDomain.isOpenDomain",
           isPartnerDomain: "$matchDomain.isPartnerDomain"
         }
   }
] )

这是可行的,但我不希望domain的字段作为array.unwind:

db.createView( "userDomainRelation", "user", [
   {
      $lookup:
         {
            from: "domain",
            localField: "domain",
            foreignField: "_id",
            as: "matchDomain"
         }
   },
   {
      $project:
         {
           _id: 1,
           email: 1,
           firstName: 1,
           lastName: 1,
           accountType: 1,
           domain: 1,
           host: "$matchDomain.host",
           isOpenDomain: "$matchDomain.isOpenDomain",
           isPartnerDomain: "$matchDomain.isPartnerDomain"
         }
   },
   {
        $unwind: "$host"
    }
] )

对于每userhost总是存在于domain中.如果我也解开isOpenDomainisPartnerDomain,我只得到isOpenDomainisPartnerDomain出现在"域"中的记录.为什么会这样?我该如何解决这个问题?我想要所有的域,即使isOpenDomainisPartnerDomain是不存在的.

db.createView( "userDomainRelation", "user", [
   {
      $lookup:
         {
            from: "domain",
            localField: "domain",
            foreignField: "_id",
            as: "matchDomain"
         }
   },
   {
      $project:
         {
           _id: 1,
           email: 1,
           firstName: 1,
           lastName: 1,
           accountType: 1,
           domain: 1,
           host: "$matchDomain.host",
           isOpenDomain: "$matchDomain.isOpenDomain",
           isPartnerDomain: "$matchDomain.isPartnerDomain"
         }
   },
   {
        $unwind: "$host"
    },
   {
        $unwind: "$isOpenDomain"
    },
   {
        $unwind: "$isPartnerDomain"
    }
] )

推荐答案

默认值$unwind behaviour是跳过空数组空值.&因此,将这些字段缺失的记录不包括在内.如果您希望记录始终存在,请使用preserveNullAndEmptyArrays: true.见this example from the docs.

More importantly,为了您的使用,您应该在您的$projectjust after the 101(视情况而定)中解开before.通过只展开matchDomain字段,它将包括其中的所有字段-而不需要preserveNullAndEmptyArrays: true部分.

db.createView( "userDomainRelation", "user", [
   {
      $lookup:
         {
            from: "domain",
            localField: "domain",
            foreignField: "_id",
            as: "matchDomain"
         }
   },
   {
      $unwind: "$matchDomain"
   },
   {
      $project:
         {
           _id: 1,
           email: 1,
           firstName: 1,
           lastName: 1,
           accountType: 1,
           domain: 1,
           host: "$matchDomain.host",
           isOpenDomain: "$matchDomain.isOpenDomain",
           isPartnerDomain: "$matchDomain.isPartnerDomain"
         }
   }
] )

如果您提供一些测试数据/示例,那么我们可以看到您实际上试图得到什么.

Mongodb相关问答推荐

如何在Mongo Aggregate Query中创建集合的对象ID数组,并在列表中查找另一个集合中的ID

MongoDB MQL,将列表一分为二,仅获取唯一值

带dropTarget的MongoDB renameCollection命令是原子的吗

如何填充Mongoose中的嵌套引用

Mongo按最大分组排序

Mongoose 聚合和多级组

mongoDB中数组中的聚合和元素

Mongodb 按数组元素聚合组

Mongodb,在一个查询中用正则表达式更新部分字符串

用Golang减go mongodb中的两个字段

在亚马逊 EC2 上托管 nodeJS/mongoose Web 应用程序

如何将查询结果(单个文档)存储到变量中?

如何在 Ruby on Rails 环境中使用 Mongoid 进行通配符搜索?

Mongo DB中的Upserting和Id问题

为多个日期范围聚合 $group

MongoDB GridFS VS 直接磁盘 IO

使用 Spring Security + Spring 数据 + MongoDB 进行身份验证

如何在 Rails 中混合使用 mongodb 和传统数据库?

在 Ubuntu 13.10 (saucy) 中安装 Mongodb PHP 扩展的最简单方法?

在 mongoose 中使用 UUID 进行 ObjectID 引用