Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Data query of MongoDB (data sorting)

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In MongoDB, the data sorting operation uses the "sort ()" function, which can be sorted in two orders: ascending (1) and descending (- 1).

Example: data sorting

> db.emp.find () .sort ({"sal":-1}) .pretty ()

{

"_ id": ObjectId ("599108433268c8e84253be2d")

"name": "bastards"

"sex": "male"

"age": 35

"sal": 8000

"loc": "Beijing"

}

But in the process of sorting, there is a way called natural sorting, sorted in the order in which the data is saved, using "$natural".

Example: natural sorting

> db.emp.find () .sort ({"natural":-1}) .pretty ()

{

"_ id": ObjectId ("599108423268c8e84253be26")

"name": "Zhao Yi"

"sex": "male"

"age": 30

"sal": 1000

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be27")

"name": "Qian er"

"sex": "female"

"age": 22

"sal": 5000

"loc": "Shanghai"

}

Sorting operations in MongoDB databases are easier to set up than traditional relational databases.

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report