How to count a cursor iteration in mongodb?

Viewed 96
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));
};

For the above code im trying to find the count of each book, journal, musicCD and magazine. IE how many times each appears in my document. The above code is able to return the whole document in a pretty format. How would i find the count of each type? (IE number of books, the number of journals, the number of musicCD and the number of magazines). Note im required to complete the task using an interation over a cursor!

an example output would be

number of books: 4
number of journals: 7
etc

blow is the js file(it is quite large) for anyone who wants it for reference.

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

db.bookshop.insert( {
    "_id":"214.56.82 12.23",
    "journal": {
        "callnum":"214.56.82",
        "issn":"S-11-123-123-456",
        "title":"Handyman",
        "vol":12,
        "issue":23,
        "price":9.85,
        "year":2017,
        "month":"February"
    }
});

db.bookshop.insert( {
    "_id":"214.56.82 12.24",
    "journal": {
        "callnum":"214.56.82",
        "issn":"S-11-123-123-456",
        "title":"Handyman",
        "vol":12,
        "issue":24,
        "price":9.85,
        "year":2017,
        "month":"August"
    }
});


db.bookshop.insert( {
    "_id":"214.56.82 12.25",
    "journal": {
        "callnum":"214.56.82",
        "issn":"S-11-123-123-456",
        "title":"Handyman",
        "vol":12,
        "issue":25,
        "price":9.85,
        "year":2017,
        "month":"October"
    }
});

db.bookshop.insert( {
    "_id":"263.18.37 5.33",
    "journal": {
        "callnum":"263.18.37",
        "issn":"D-10-123-124-456",
        "title":"Information processing",
        "vol":5,
        "issue":33,
        "price":15.90,
        "year":2018,
        "month":"January"
    }
});

db.bookshop.insert( {
    "_id":"263.18.37 5.34",
    "journal": {
        "callnum":"263.18.37",
        "issn":"D-10-123-124-456",
        "title":"Information processing",
        "vol":5,
        "issue":34,
        "price":15.90,
        "year":2018,
        "month":"May"
    }
});

db.bookshop.insert( {
    "_id":"285.32.74 17.65",
    "journal": {
        "callnum":"285.32.74",
        "issn":"C-9-123-456-666",
        "title":"Mathmetics and Computing",
        "vol":17,
        "issue":65,
        "price":35.90,
        "year":2018,
        "month":"December"
    }
});

db.bookshop.insert( {
    "_id":"285.32.74 17.66",
    "journal": {
        "callnum":"285.32.74",
        "issn":"C-9-123-456-666",
        "title":"Mathmetics and Computing",
        "vol":17,
        "issue":66,
        "price":35.90,
        "year":2019,
        "month":"February"
    }
});

db.bookshop.insert( {
    "_id":"321.12.76",
    "musicCD": {
        "callnum":"321.12.76",
        "title":"Music",
        "producer":"ABC Pty Ltd",
        "year":2010,
        "category":"Pop",
        "price":12.80
    }
});

db.bookshop.insert( {
    "_id":"342.22.42",
    "musicCD": {
        "callnum":"342.22.42",
        "title":"Stars",
        "producer":"BBC Pty Ltd",
        "year":2012,
        "category":"Classic",
        "price":12.80
    }
});

db.bookshop.insert( {
    "_id":"332.17.25",
    "musicCD": {
        "callnum":"332.17.25",
        "title":"Wiggle Wiggle",
        "producer":"Wiggle Pty Ltd",
        "year":2005,
        "category":"Kids",
        "price":10.50
    }
});

db.bookshop.insert( {
    "_id":"402.12.1",
    "magazine": {
        "callnum":"402.12.1",
        "title":"ORACLE",
        "publisher":"Julia McVeigh",
        "topic":"Database",
        "price":4.50,
        "year":2017,
        "month":"June",
        "contents":[
            {"event":[{"current":{"page":14}}, {"upcoming":{"page":15}}]},
            {"columns":[
                {"title":"Getting good service", "page":17, "author":"Jeff Spicer"},
                {"title":"Behind Unbreakable", "page":19, "author":"Ken Jacobs"},
                {"title":"Getting good", "page":21, "author":"Rich Niemiec"},
                {"title":"Building and using portals", "page":25, "author":"Robert Hall"},
                {"title":"Blocks, messages, rows", "page":29, "author":"Tom Kyte"}
            ]},
            {"upfront":{
                "Oracle monitor":{
                    "page":39,
                    "titles":["JDeveloper arrives", "Oracle and network", "Supply chain goes with flow"]
                }, 
                "Did you know":{"page":40}, 
                "Share pool":{
                    "page":53,
                    "titles":["Systems expands content-management", "ArtinSoft automates migration to Java"]
                }
            } }
        ]
    }
});

db.bookshop.insert( {
    "_id":"402.12.2",
    "magazine": {
        "callnum":"402.12.2",
        "title":"ORACLE",
        "publisher":"Julia McVeigh",
        "topic":"Database",
        "price":4.50,
        "year":2019,
        "month":"July",
        "contents":[
            {"event":[{"current":{"page":9}}, {"upcoming":{"page":10}}]},
            {"columns":[
                {"title":"Making integration", "page":15, "author":"Jeff Spicer"},
                {"title":"Oracle retrospective", "page":21, "author":"Rich Niemiec"},
                {"title":"Stretching the Web", "page":27, "author":"Robert Hall"},
                {"title":"Beyond Init.ora and SYS", "page":29, "author":"Tom Kyte"}
            ]},
            {"upfront":{
                "Oracle monitor":{
                    "page":39,
                    "titles":["Oracle information architecture", "Certifications debut at OpenWorld", "Big new for small business", "V-business on the horizon"]
                }, 
                "Did you know":{"page":40}, 
                "Share pool":{
                    "page":47,
                    "titles":["Luminate. Net intelligent database management", "Unicenter supports Oracle", "SDA's Web database objects"]
                }
            } }
        ]
    }
});

1 Answers

You could do that with a single iteration on the cursor : If the fields (book, musicCD etc.) are considered for counting, which I understand is what you are looking for.

var cursor = db.bookshop.find(); //find will give the cursor as well
var bookCount = 0;
var journalCount = 0;
var musicCDCount = 0;
var magazineCount = 0;
while (cursor.hasNext()) {
  var current = cursor.next();
  if (typeof current["book"] !== "undefined") {
    bookCount++;
  }

  if (typeof current["journal"] !== "undefined") {
    journalCount++;
  }

  if (typeof current["musicCD"] !== "undefined") {
    musicCDCount++;
  }

  if (typeof current["magazine"] !== "undefined") {
    magazineCount++;
  }
}
print("number of books: " + bookCount);
print("number of journals: " + journalCount);
print("number of musicCDs: " + musicCDCount);
print("number of magazines: " + magazineCount);
Related