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!