查找文档及其参考文档

时间:2019-09-25 05:46:18

标签: node.js mongodb mongoose mongodb-query

我在尝试使用mongodb中的聚合管道填充相关字段时遇到了麻烦,普通关系也可以工作(我的意思是对其他集合中的oid的oid引用),但是当对象数组被其属性之一引用时会发生什么子文档。如果我不清楚,请在这里稍作介绍。

假设我具有以下架构:

Profile {
    _id: {
        type: mongoose.Schema.Types.ObjectId
    },
    Gender: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Gender"
    },
    PhoneNo: [
        Value: {
            type: String
        },
        PhoneType: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "PhoneType"
        }
    ]
}

PhoneType {
    _id: {
        type: mongoose.Schema.Types.ObjectId
    },
    Name: {
        type: String
    }
}

Gender {
    _id: {
        type: mongoose.Schema.Types.ObjectId
    },
    Name: {
        type: String
    }
}

所以,我想得到如下结果:

    {
        _id: $oid,
        Gender: {Value:"Masculine"},
        PhoneNo: {
            Value: "000000",
            PhoneType: {
                _id: $oid
                Name: "Cell"
            }
        }
    },
    {
        _id: $oid,
        Gender: {Value:"Feminine"},
        PhoneNo: {
            Value: "999999",
            PhoneType: {
                _id: $oid
                Name: "Work"
            }
        }
    }

在Gender中查找效果很好,但是当我尝试查找PhoneNo时,我丢失了 value 属性。

我得到的是:

  

失去对字段/属性的关注。

    {
       _id: $oid,
       Gender: {Value:"Masculine"},
       PhoneNo: [{
         PhoneType: {
            _id: $oid
            Name: "Cell"
         }
       }]
    },

    {
       _id: $oid,
       Gender: {Value:"Feminine"},
       PhoneNo: [{
         PhoneType: {
            _id: $oid
            Name: "Work"
         }
       }]
    }

这是我使用的代码:

{
  from: 'PhoneType',
  'let': {"ids":"$PhoneNo.PhoneType"},
  "pipeline": [
    { "$match": { "$expr": { "$in": ["$_id", "$$ids"] } } },
  ],
  as: "PhoneNo"
}

我该怎么做? :S

1 个答案:

答案 0 :(得分:1)

所以这是您的问题的演练,我们还在lookup let声明中包含Values,因为它将存储在数组中,并且在项目阶段,我们将获取indexOf Values数组,其中$$ ROOT的id和ID匹配

https://mongoplayground.net/p/UUXus3N3ncw

输入集合:

"user": [
    {
      _id: {
        type: "1"
      },
      Gender: 12,
      PhoneNo: [
        {
          Value: "name",
          PhoneType: 21
        },
        {
          Value: "name2",
          PhoneType: 212
        }
      ]
    }
  ],
  "gender": [
    {
      "_id": 12,
      "Name": "Male"
    }
  ],
  "PhoneType": [
    {
      "_id": 21,
      name: "Sus",
      val: "750"
    },
    {
      "_id": 212,
      name: "Suss",
      val: "7500"
    }
  ]

总管道:

user.aggregate([
  {
    $lookup: {
      from: "PhoneType",
      "let": {
        "ids": "$PhoneNo.PhoneType",
        "val": "$PhoneNo.Value",

      },
      "pipeline": [
        {
          "$match": {
            "$expr": {
              "$in": [
                "$_id",
                "$$ids"
              ]
            }
          }
        },
        {
          $project: {
            _id: 0,
            Value: {
              $arrayElemAt: [
                "$$val",
                {
                  $indexOfArray: [
                    "$$ids",
                    "$$ROOT._id"
                  ]
                }
              ]
            },
            PhoneType: "$$ROOT"
          }
        }
      ],
      as: "PhoneNo"
    }
  }
])

输出结果:

[
  {
    "Gender": 12,
    "PhoneNo": [
      {
        "PhoneType": {
          "_id": 21,
          "name": "Sus",
          "val": "750"
        },
        "Value": "name"
      },
      {
        "PhoneType": {
          "_id": 212,
          "name": "Suss",
          "val": "7500"
        },
        "Value": "name2"
      }
    ],
    "_id": {
      "type": "1"
    }
  }
]