Mongodb document query operation (III)

Time:2021-12-2

We have introduced two articles on query in mongodb. In this article, we will introduce another query conceptcursor

This is the seventh article in mongodb series. Understanding the previous articles will help you better understand this article:


1.Installing mongodb on Linux
2.Mongodb basic operation
3.Mongodb data type
4.Mongodb document update operation
5.Mongodb document query operation (I)
6.Mongodb document query operation (II)


basic operation

The concept of cursor exists in many places, such as resultset in JDBC in Java, cursor in Android, etc. there are similar concepts in mongodb. When we call the find method, we can return a cursor, as follows:

var cursor = db.sang_collect.find();

There are hasnext() and next() methods in the cursor. The combination of these two methods can be used to traverse the results, as follows:

while(cursor.hasNext()){
    print(cursor.next())
}

The next () method can obtain each document queried, as follows:

{
    "_id" : ObjectId("59f299579babb96c21ddc9e8"),
    "x" : 0.0,
    "y" : 1000.0
}

/* 2 */
{
    "_id" : ObjectId("59f299579babb96c21ddc9e9"),
    "x" : 1.0,
    "y" : 999.0
}

If I only want to get a field in the document, I can do it as follows:

while(cursor.hasNext()){
    print(cursor.next().y)
}

Cursor also implements the iterator interface in JavaScript, so we can also directly call the foreach method to traverse:

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

When we call the find method to obtain the cursor, the shell will not query the database immediately, but will load the data when it is actually used, which is somewhat similar to the lazy loading in the database framework. The shell will obtain the first 100 results or the first 4MB of data (whichever is the smallest), Then, when we call hasnext and next, the shell does not need to connect to the database. It directly returns the queried data one by one. After all the 100 or 4MB data are returned, the shell will again initiate a request to ask mongodb for data.

limit

Limit is a method in cursor to limit the number of returned results. For example, I only want to get the first three results of the query, as follows:

var cursor = db.sang_collect.find().limit(3)

skip

Skip is also a method in cursor, which is used to indicate the number of records skipped. For example, I want to obtain records 2 to 5, as follows:

var cursor = db.sang_collect.find().skip(2).limit(4)

Skip the first two (0 and 1) and get the next four data. The combination of skip and limit is somewhat similar to the limit in MySQL and can be used for paging, but this paging method is inefficient.

sort

Sort is used to implement sorting functions, such as sorting by X, as follows:

var cursor = db.sang_collect.find().sort({x:-1})

1 indicates ascending order, – 1 indicates descending order.

OK, that’s all for the query in mongodb. If you have any questions, please leave a message for discussion.

reference material:

1. Authoritative guide to mongodb, 2nd Edition

More information on the official account:

Mongodb document query operation (III)