Computer >> Máy Tính >  >> Lập trình >> MongoDB

({$ Natural:1}) trong MongoDB là gì?

({$ Natural - 1}) hoạt động giống như LIFO (CUỐI CÙNG ĐẦU TIÊN RA NGOÀI), có nghĩa là tài liệu được chèn lần cuối sẽ được hiển thị đầu tiên.

Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -

> db.demo614.insertOne ({"CountryName":"US"}); {"Recognition":true, "insertId":ObjectId ("5e988cddf6b89257f5584d8e")}> db.demo614.insertOne ({"CountryName" :"UK"}); {"Recognition":true, "insertId":ObjectId ("5e988ce0f6b89257f5584d8f")}> db.demo614.insertOne ({"CountryName":"AUS"}); {"Recognition":true, "insertId":ObjectId ("5e988ce3f6b89257f5584d90")}> db.demo614.insertOne ({"CountryName":"IND"}); {"Recog":true, "insertId":ObjectId ("5e988cebf6b89257f5584d91")}> 

Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -

> db.demo614.find (); 

Điều này sẽ tạo ra kết quả sau -

 {"_id":ObjectId ("5e988cddf6b89257f5584d8e"), "CountryName":"US"} {"_id":ObjectId ("5e988ce0f6b89257f5584d8f"), "CountryName":"UK"} {"_id":ObjectId ( "5e988ce3f6b89257f5584d90"), "CountryName":"AUS"} {"_id":ObjectId ("5e988cebf6b89257f5584d91"), "CountryName":"IND"} 

Sau đây là truy vấn để làm việc với ({$ natural:1}) -

> db.demo614.find (). sort ({$ natural:-1}) 

Điều này sẽ tạo ra kết quả sau -

 {"_id":ObjectId ("5e988cebf6b89257f5584d91"), "CountryName":"IND"} {"_id":ObjectId ("5e988ce3f6b89257f5584d90"), "CountryName":"AUS"} {"_id":ObjectId ( "5e988ce0f6b89257f5584d8f"), "CountryName":"UK"} {"_id":ObjectId ("5e988cddf6b89257f5584d8e"), "CountryName":"US"}