Am working on a query that finds the subject title, subject type and credit value of subjects with the credit value of 3. The output has to be then be listed out in ascending order. The query also must only be done via the use of aggregate.
Here is the query I have crafted:
db.Subject.aggregate([
{$match:{"subject.credit":"3"}},
{$project:{"subject.title":1, "subject.type":1, "subject.credit":1}},
{$sort:{"subject.title":1}}
])
This does not seem to net any output at the moment. May I ask what is wrong with the query as well as on how I can filter the output in aggregation.
Here is a sample chunk of the database containing an entry that I am trying to find.
db.Subject.insert(
{
"_id":ObjectId(),
"subject": {
"subCode":"CSCI103",
"subTitle":"Algorithm and Problem Solving",
"credit":3,
"type":"Core",
"assessments": [
{ "assessNum": 1,
"weight":10,
"assessType":"Assignment",
"description":"Problem Solving and Invariant" },
{ "assessNum": 2,
"weight":10,
"assessType":"Assignment",
"description":"Assignment 1 - Sorting and Seaarching, Linked Lists, and Stack and Queues" },
{ "assessNum": 3,
"weight":10,
"assessType":"Assignment",
"description":"Assignment 2 - Recursion, Trees, and Algorithmic Strategies" },
{ "assessNum": 4,
"weight":10,
"assessType":"Test/Quiz",
"description":"Closed-book Class Test" },
{ "assessNum": 5,
"weight":60,
"assessType":"Examination",
"description": "Closed-book Final Examination" }
],
"book": [
{ "ISBN":"13:978-0-13-231681-1",
"bookType":"textbook",
"bookTitle":"Introduction to the Design and Analysis of Algorithms",
"edition":3,
"yearPub":2012,
"publisher":"Pearson",
"author": [ "Anany Levitin" ] },
{ "ISBN":"13:978-0-13-231681-1",
"bookType":"reference",
"edition":3,
"yearPub":2005,
"publisher":"Pearson",
"author": [ "B A Forouzan", "D S Malik", "M K Sen Thomson" ] }
]
}
}
)
{$match:{"subject.credit":"3"}}- the value3is a number data type in the collection's document, I say that you use the same data types when comparing.subject.title. Sorting is (probably) the most expensive stage. And moving sort before$project. Also,"3"!=3:-)