MongoDB:如何像MySQL中的fields关键字一样对结果中的记录进行排序
MongoDB does not provide a built-in way to order the records in the results like the "fields" keyword in MySQL. This difference in functionality has caused some confusion for users who are accustomed to using the "fields" keyword in MySQL.
To address this issue, one user suggests using the find() method in MongoDB with the projection parameter to specify the fields to include in the results. They provide an example code snippet:
db.users.find({},{_id:0,"accounts":1,"name":1});
However, it is mentioned that they have never tried ordering the fields in MongoDB using this method. Another user then expresses the desire to sort the results based on a specific order of IDs.
In conclusion, the issue arises from the difference in functionality between MongoDB and MySQL, where MongoDB lacks a direct way to order fields in the results. The suggested solution involves using the find() method with the projection parameter to specify the desired fields, but it is unclear whether this method can be used to order the fields as well.