0

Am currently working on a query that requires me to list the ISBN and titles of book of all entries that were published in 2012.

My query:

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

Output:

{
    "_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"
            }
        ]
    }
}

This however seems to give all the ISBN and titles of an entry as long one of the books is published on 2012. How can I change this so that only the books that were published will be outputted.

Entry sample:

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" ] },

            ]
        }
}
)

So in the sample entry shown above, only Starting out with C++ should appear as it was published on year 2012 unlike the entry above which was published on year 2013.

1 Answer 1

2

Try

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
    }
  }
])
  • First project filters books from 2012
  • Second project removes all other fields
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for the late reply. Your answer works perfectly!
good to know my friend, good luck! @Frio_Penitencia

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.