首页 > 解决方案 > MongoDb 在仅使用聚合过滤输出时需要帮助

问题描述

我目前正在处理一个查询,要求我列出 2012 年出版的所有条目的 ISBN 和书名。

我的查询:

db.Subject.aggregate( [{$match:{"subject.book.yearPub":2012}},{$project:{"subject.book.ISBN":1,"subject.book.bookTitle":1 }}] ).pretty()

输出:

{
    "_id" : ObjectId("5fb1ea7658bf7f7d4e10771d"),
    "subject" : {
        "book" : [
            {
                "ISBN" : "13:978-0-13-231681-1",
                "bookTitle" : "Introduction to the Design and Analysis of Algorithms"
            },
            {
                "ISBN" : "13:978-0-13-231681-1"
            }
        ]
    }
}
{
    "_id" : ObjectId("5fb1ea7658bf7f7d4e10771e"),
    "subject" : {
        "book" : [
            {
                "ISBN" : "13:978-1-133-52635-3",
                "bookTitle" : "C++ Programming - Program design including data structure"
            },
            {
                "ISBN" : "13:978-0-273-75983-6",
                "bookTitle" : "Starting Out With C++: From Control Structures through Objects"
            }
        ]
    }
}
{
    "_id" : ObjectId("5fb1ea7658bf7f7d4e10771f"),
    "subject" : {
        "book" : [
            {
                "ISBN" : "10:1-4390-4023-0",
                "bookTitle" : "Data Structures Using C++"
            },
            {
                "ISBN" : "13:978-1-133-52635-3",
                "bookTitle" : "C++ Programming - Program design including data structure"
            },
            {
                "ISBN" : "13:978-0-273-75983-6",
                "bookTitle" : "Starting Out With C++: From Control Structures through Objects"
            }
        ]
    }
}
{
    "_id" : ObjectId("5fb1ea7658bf7f7d4e107721"),
    "subject" : {
        "book" : [
            {
                "ISBN" : "13:978-0-13-231681-1",
                "bookTitle" : "Introduction to the Design and Analysis of Algorithms"
            },
            {
                "ISBN" : "978-0-262-53305-8",
                "bookTitle" : "Introduction to Algorithms"
            }
        ]
    }
}

然而,只要其中一本书于 2012 年出版,这似乎给出了条目的所有 ISBN 和标题。我该如何更改这一点,以便只输出已出版的书籍。

参赛样本:

db.Subject.insert(
{
"_id":ObjectId(),
"subject": {
        "subCode":"CSCI114",
        "subTitle":"Procedural Programming",
        "credit":3,
        "type":"Core",
        "assessments": [
                { "assessNum": 1,
                  "weight":5,
                  "assessType":"Assignment",
                  "description":"Assignment 1 - Basic Concepts: Sequential Designs" },
                { "assignNum": 2,
                  "weight":5,
                  "assessType":"Assignment",
                  "description":"Assignment 2 - Control structures: Selection Designs" },
                { "assessNum": 3,
                  "weight":5,
                  "assessType":"Assignment",
                  "description":"Assignment 3 - Repetition Designs and Functions I" },
                { "assessNum": 4,
                  "weight":15,
                  "assessType":"Test/Quiz",
                  "description":"Closed-book Class Test" },
                { "assessNum": 5,
                  "weight": 10,
                  "assessType":"Test/Quiz",
                  "description":"Laboratory Test" },
                { "assessNum": 6,
                  "weight": 10,
                  "assessType":"Test/Quiz",
                  "description": "Closed-book Class Test" },
                { "assessNum": 7,
                  "weight":50,
                  "assessType":"Examination",
                  "description": "Closed-book Final Examination" }
            ],
        "book": [
                { "ISBN":"13:978-1-133-52635-3",
                  "bookType":"reference",
                  "bookTitle":"C++ Programming - Program design including data structure",
                  "edition":6,
                  "yearPub":2013,
                  "publisher":"CENGAGE Learning",
                  "author": [ "Malik D S" ] },
                { "ISBN":"13:978-0-273-75983-6",
                  "bookType":"reference",
                    "bookTitle":"Starting Out With C++: From Control Structures through Objects",
                  "edition":7,
                  "yearPub":2012,
                  "publisher":"Addison-Wesley",
                  "author": [ "Tony Gaddis" ] },

            ]
        }
}
)

因此,在上面显示的示例条目中,应该只显示从 C++ 开始,因为它是在 2012 年发布的,而上面的条目是在 2013 年发布的。

标签: mongodb

解决方案


尝试

db.Subject.aggregate([
  {
    $match: {
      "subject.book.yearPub": 2012
    }
  },
  {
    $project: {
      result: {
        $filter: {
          input: "$subject.book",
          as: "b",
          cond: {
            $eq: [
              "$$b.yearPub",
              2012
            ]
          }
        }
      }
    }
  },
  {
    $project: {
      "result.ISBN": 1,
      "result.bookTitle": 1
    }
  }
])
  • 第一个项目过滤 2012 年的书籍
  • 第二个项目删除所有其他字段

推荐阅读