MongoDB cursor (Cursor)

 

Cursor definition:

In layman's terms, the cursor is the return resource interface of the query, through which it can be traversed one by one.

 

Declare the cursor:

var c1 = db.user.find({age:{$lt:5}});

c1.hasNext();//Determine whether the cursor has reached the end

c1.next();//Remove the next unit of the cursor

 

eg:

var c1 = db.user.find({age:{$lt:5}});

while (c1.hasNext()) {

   print(tojson(c1.next()));

   //or printjson(c1.next());

}

 

related functions:

1, tojson ()

 

2,printjson()

 

3, forEach() traverses the cursor

    forEach(callback function)

eg1:

var c1 = db.user.find({age:{$lt:5}});

c1.forEach(printjson);

eg2:

var c1 = db.user.find({age:{$lt:5}});

c1.forEach(function(obj){print(obj.name)});

4, toArray() converts the cursor to an array

eg2:

var c1 = db.user.find({age:{$lt:5}});

c1.toArray();

 

eg2:

var c1 = db.user.find({age:{$lt:5}});

var a1 = c1.toArray();

printjson(a1[3]);

 

5, skip() skips the number of rows, limit() the number of records queried

eg:

var c1 = db.user.find({age:{$lt:5}}).skip(3).limit(4);

c1.forEach(printjson)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326118600&siteId=291194637