I am not able to understand why the inserted MongoDB document although it contains items, the code is not able to iterate over. The cursor object itself is not null. I am able to retrieve the documents using db.newmongo.find()
var url = 'mongodb://localhost:27017/test';
MongoClient.connect(url, function(err, db) {
db.collection("newmongo").insert([{"name": "XXX", "age": "50"},
{"name": "YYY", "age": 43},
{"name": "ZZZ", "age": 27},
{"name": "AAA", "age": 29},
{"name": "BBB", "age": 34}]);
console.log("Connected correctly to server.");
var cursor=db.collection('newmongo').find();
console.log(cursor); // This gets logged
cursor.each(function(err, doc) {
if (doc != null) {
console.log('Document found');
} else {
console.log('Document not found');
}
});