A brief analysis of the use of mongodb database cursors

  • 2020-05-09 19:32:08
  • OfStack

Examples of cursor usage in mongodb are as follows:
 
Suppose you do the following:


db.XXX.remove();
db.XXX.insert({"name":"BuleRiver1", "age":27});
db.XXX.insert({"name":"BuleRiver2", "age":23});
db.XXX.insert({"name":"BuleRiver3", "age":21});

Return 1 cursor using find() :


var cursor = db.XXX.find();
while (cursor.hasNext()) {
  obj = cursor.next();
  print(obj.name);
}

Loop through using the cursor's forEach() :


cursor.forEach(function(x) {
  print(x.name);
});

Related articles: