How to insert to a MongoDB collection with a position

后端 未结 1 475
眼角桃花
眼角桃花 2021-01-23 18:13

I\'m trying to insert different items to a MongoDB collection, but I want to the item to be inserted at the top of the array so when retrieving the collection items I can get th

相关标签:
1条回答
  • 2021-01-23 18:38

    You should just write a query for the data that you want. The $orderby operator will sort the data any way you like.

    collection.find({ $query: {}, $orderby: { postid: -1 } });
    

    If you really just need the last item, you could limit the query to one result as well. Note that this version uses the sort function as well.

    collection.find().sort({ postid: -1 }).limit(1);
    
    0 讨论(0)
提交回复
热议问题