0

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

            ]
        }
}
)
3
  • 1
    {$match:{"subject.credit":"3"}} - the value 3 is a number data type in the collection's document, I say that you use the same data types when comparing. Commented Nov 17, 2020 at 6:07
  • 1
    if you need this for a "real time" app, I would consider (test) creating an index in subject.title. Sorting is (probably) the most expensive stage. And moving sort before $project. Also, "3"!=3 :-) Commented Nov 17, 2020 at 6:44
  • Thx for the feedback, I was not aware that " " was causing the issues shown above. Commented Nov 17, 2020 at 8:39

1 Answer 1

1
db.Subject.aggregate([{$match:{"subject.credit": 3}},
   {$project:{"subject.title": 1, "subject.type": 1, "subject.credit": 1}},
   {$sort:{"subject.title": 1}}]) 

give 3 as a number

Sign up to request clarification or add additional context in comments.

Comments

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.