Cursor usage in MongoDB- Part 12
What is a cursor? Well in database terminology, a curser is basically a pointer to your data, and it can be moved forward or backwards. Basically it’s a way of looking at your data using a specific variable to point to a location within your data. Well let’s create one. We create a variable; which will just going to call acursor. We are going to set it equal to the response of a find() operation.
var acursor = db.products.find() — To create cursor acursor.count()
Now try acursor.explain(). It’s going to tell you this is a basic cursor, it’s not multikey, it has five objects in it, it wasn’t put in any particular order, and give you some information about the server it’s on, and all kinds of things, very helpful information here. Now, you may wish to move back and forth in your cursor. So acursor.next(), and it moves to the very next value, since I started at nothing, that’s going to be my first value. Now, if you keep moving you’ll reach the end, and you will generate an error. So how about let’s find out if there is a next value before going any further, acursor.hasNext() – true, so I haven’t reached the end yet.
acursor.hasNext()