MongoDB sorting

前端 未结 4 1585
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 13:23

I want implement a \"bump\" feature for topics. Once a topic is bumped, it will have a new \"bump_date\" field. I want to sort it so that when there is a \"bump_date\" fie

4条回答
  •  旧时难觅i
    2020-12-13 13:39

    Sorting in MongoDB is done like so:

    db.collection.find({ ... spec ... }).sort({ key: 1 })

    where 1 is ascending and -1 is descending.

    In your specific example: db.topics.find().sort({ bump_date: 1 }), although it might be better to call it something like "updated_at".

    You'll also definitely want to put an index on your "bump_date" field.

    • sorting: http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order
    • indexes: http://www.mongodb.org/display/DOCS/Indexes

提交回复
热议问题