0

My current assignment asks

The queries listed below must be implemented by iterations over a cursor. 

Im confused on what iterations over a cursor means? Ive tried researching on my own to understand this concept but im still a bit lost of the idea.

The first question is

 List the title and total number of keywords for each book. If a book has no
keywords, the total number of keywords must be 0 (zero). 

Here is the relevent JS script

db.bookshop.insert( {
    "_id":"185.3.16",
    "book": {
        "callnum":"185.3.16",
        "isbn":"1-292-06118-9",
        "title":"Database Systems",
        "authors":[
            {
                "fname":"Thomas",
                "lname":"Connolly"},
            { 
                "fname":"Carolyn",
                "lname":"Begg"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2015,
        "price":136.99,
        "topic":"Computer Science",
        "description":"This is the 6th edition. You can register online to access the examples",
        "keywords":["Database", "XML", "Distributed"]
        }
});

db.bookshop.insert( {
    "_id":"163.24.12",
    "book": {
        "callnum":"163.24.12",
        "isbn":"1-123-456-810",
        "title":"Core Java",
        "authors":[
            {
                "fname":"Horstmann",
                "lname":"Cornell"}
        ],
        "publisher":"PH Pty Ltd",
        "year":2012,
        "price":142.90,
        "topic":"Computer Science",
        "description":"It covers JAVA programming and JAVA script",
        "keywords":["JAVA", "XML", "Script"]
    }
});

db.bookshop.insert( {
    "_id":"123.45.67",
    "book": {
        "callnum":"123.45.67",
        "isbn":"1-123-456-789",
        "title":"Algorithms",
        "authors":[
            {
                "fname":"James",
                "lname":"Bond"},
            {
                "fname":"Harry",
                "lname":"Potter"},
            {
                "fname":"William",
                "lname":"Stallings"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2013,
        "price":65.85,
        "topic":"Computer Science",
        "description":"It contains algorithms and their applications. You can download examples from the website"
    }
});
db.bookshop.insert( {
    "_id":"134.41.33",
    "book": {
        "callnum":"134.41.33",
        "isbn":"1-213-431-770",
        "title":"C++ Programming",
        "authors":[
            {
                "fname":"Larry",
                "lname":"Peterson"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2010,
        "price":74.90,
        "topic":"Computer Science",
        "description":"C++ programming and its applications",
        "keywords":["C++", "Class", "Overloading", "Inheritance"]
    }
})

this is my attempt

var myCursor = db.bookshop.aggregate([]).pretty();

while (myCursor.hasNext()) {
   var x = myCursor.next();
   if(x.book)
   print(tojson(x));
};

while (myCursor.hasNext()) {
   var x = myCursor.next();
   if(x.journal)
   print(tojson(x));
};

while (myCursor.hasNext()) {
   var x = myCursor.next();
   if(x.musicCD)
   print(tojson(x));
};

while (myCursor.hasNext()) {
   var x = myCursor.next();
   if(x.magazine)
   print(tojson(x));
};

this attempt prints but does not count.

If someone could possible explain the concept through the question ive provided that would be greatly appreciated!

DanielLu
  • 23
  • 5
  • maybe [this](https://stackoverflow.com/questions/21387969/mongodb-count-the-number-of-items-in-an-array) help, you should use aggregate, then count. – prhmma Oct 23 '19 at 09:34

1 Answers1

0

what iterations over a cursor means?

lets first see what is cursor?

In simple words, a cursor is a result after the query is run.

So the data/result returned after the query is executed is a cursor.

read more about cursor

From the question description provided by you, this is my understanding:

You have to form a query to accomplish your tasks and iterate over the result of the query to output the results.

The script provided by you is to insert those data into DB, and then perform your operations/tasks on this inserted data.

What will be the query?

I will leave those to you,

try it...

If you need help,

update your question with code samples that you have tried...

AFTER COMMENTS:

Here is the solution:

db.bookshop.aggregate({$match:{}},
    {$project:{"book.title":1, keywords: {$ifNull: ["$book.keywords", []]}}},
    {$project:{"book.title":1, noOfKeywords:{$size:"$keywords"}}})

I used $ifNull operator, to check if keywords array exists in book object in each document,

in this line

{$ifNull: ["$book.keywords", []]}

if the keywords array is null or does not exists then it will replace that particular document's keyword field with [] in book object.

Then in the next $project stage, I used $size operator to count the size of the array and that is your answer.

niranjan_harpale
  • 2,048
  • 1
  • 17
  • 21
  • hey dude ive updated my question with an attempt. I just need help with the count. @niranjan harpale – DanielLu Oct 24 '19 at 08:52