排除 $lookup 聚合中的字段 [英] exclude fields in $lookup aggregation

查看:45
本文介绍了排除 $lookup 聚合中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 3 个集合之间进行查询,我想在输出中的任何地方排除 _id

I am querying between 3 collections I want to exclude _id everywhere in output

我的输出是:

{
    "_id" : ObjectId("5b6aed5f9bcdb5d4ae64aef5"),
    "userID" : "1",
    "skills" : [ 
        {
            "_id" : ObjectId("5b766b5f1365a4940bb6050f"),
            "skillID" : "javaid",
            "skillname" : "जावा",
            "languageID" : "hindiid"
        }, 
        {
            "_id" : ObjectId("5b766b8c1365a4940bb60535"),
            "skillID" : "pythonid",
            "skillname" : "पायथन",
            "languageID" : "hindiid"
        }
    ],

    "gender" : {
        "_id" : ObjectId("5b7687cd2a2329043e2383d5"),
        "genderID" : "femaleid",
        "gendername" : "महिला",
        "languageID" : "hindiid"
    }
}

查询:

db.User.aggregate([
  { "$match": { "userID":"1" }},
  { "$lookup":{
    "from": "Skill",
    "pipeline": [
      { "$match": { "languageID": "hindiid", "skillID": { "$in": [ "javaid","pythonid" ] }}},
    ],
    "as": "skills"
  }},
  { "$lookup": {
    "from": "Gender",
    "pipeline": [
      { "$match": { "languageID": "hindiid", "genderID" : "femaleid" }},
    ],
    "as": "gender"
  }},
  { "$unwind": { "path": "$gender", "preserveNullAndEmptyArrays": true }},
  { "$project": { "userID": 1, "skills": 1, "gender": 1 }}
]) 

在每个对象的输出中都有 _id.skill 列表的示例每个对象都有 _id 我想排除 _id> 无处不在.我如何排除?

In output for every object has _id.Example for skill list every object has _id i want exclude _id field every where. How I can exclude?

推荐答案

在 mongodb 3.6 中可以使用投影 ($project) 在 $lookup 管道......像这样的事情

In mongodb 3.6 you can use projection ($project) inside $lookup pipeline... Something like this

db.User.aggregate([
  { "$match": { "userID":"1" }},
  { "$lookup":{
    "from": "Skill",
    "pipeline": [
      { "$match": { "languageID": "hindiid", "skillID": { "$in": [ "javaid","pythonid" ] }}},
      { "$project": { "_id": 0 }}
    ],
    "as": "skills"
  }}
])

这篇关于排除 $lookup 聚合中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆