In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is the meaning of the cursor of MongoDB". In the daily operation, I believe that many people have doubts about the meaning of the cursor of MongoDB. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt of "what is the meaning of the cursor of MongoDB?" Next, please follow the editor to study!
The so-called cursor means that the data can be operated one by one, very similar to ResultSet data processing, in the MongoDB database, the control of the cursor is very simple, only need to use the find () function to return the cursor. For the returned cursor, if you want to operate, you can use two functions:
● determines whether there is the next line of data: lastNext ()
● fetches the current data: next ().
> var cursor=db.emp.find ()
> cursor.hasNext ()
True
> cursor.next ()
{
"_ id": ObjectId ("599294320184ff511bf02b97")
"name": "Zhao Yi"
"sex": "male"
"age": 30
"sal": 1000
"loc": "Beijing"
}
The above is the operation of the cursor, but it is actually impossible to use it this way, because you have to use a loop to output the content.
Example: write specific operation code
> var cursor=db.emp.find ()
> while (cursor.hasNext ()) {
... Var doc=cursor.next ()
... Print (doc.name)
...}
Zhao Yi
Qian er
Sun San
Li Si
Friday
Wu Liu
Zheng Qi
Son of a bitch
Liu A
Liu B
Liu C
Liu D
Liu E
Lin A
Lin B
Lin C
Lin D
> var cursor=db.emp.find ()
> while (cursor.hasNext ()) {var doc=cursor.next (); print (doc);}
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
[object BSON]
It is equivalent to the control of each data separately, line by line. When the cursor data is taken out, in fact, each row of data returns an object, so if you need to display the data in the form of JSON, you can use the printjson () function.
> var cursor=db.emp.find ()
> while (cursor.hasNext ()) {var doc=cursor.next (); printjson (doc);}
{
"_ id": ObjectId ("599294320184ff511bf02b97")
"name": "Zhao Yi"
"sex": "male"
"age": 30
"sal": 1000
"loc": "Beijing"
}
Of all known databases, only MongoDB's cursor operation is the simplest and most intuitive.
At this point, the study of "what is the meaning of the cursor of MongoDB" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.