問題描述
我正在查詢一個(gè)集合并根據(jù)一個(gè)屬性對結(jié)果進(jìn)行排序.有些文檔還沒有該屬性值,即 null.在排序和限制(asc 或 desc)之后,我想在結(jié)果末尾保留具有空值的文檔.Mongoose 中是否有一種簡單的方法可以使用單個(gè)查詢來做到這一點(diǎn)?如果沒有,我如何分別使用兩個(gè)查詢,因?yàn)槲冶仨毾拗平Y(jié)果以及分頁?
I am querying a collection and sorting results based on a property. Some documents don't the that property value yet i.e. null. I want to keep the documents with null values at the end of the results after sorting and limiting (asc or desc). Is there a simple way in Mongoose to do that using a single query? If not, how can I use two queries separately since I have to limit the results as well for pagination?
我想根據(jù)metrics.cpe"按升序排序,但希望 null 或 0 值位于列表末尾.示例:
I want to sort based on 'metrics.cpe' in ascending order, but want null or 0 values to be at the end of the list. Example:
示例文檔
推薦答案
我不確定我要說的解決方案.我無法測試這一點(diǎn),因?yàn)槲椰F(xiàn)在沒有設(shè)置 mongo db,但我認(rèn)為你可以使用 <collection>.aggregate
以及 $project
和 $sort
來實(shí)現(xiàn)這一點(diǎn).
Am not sure about the solution am about to say. I cant test this out as I dont have a mongo db set right now, but I think that you can use <collection>.aggregate
along with $project
and $sort
to achieve this.
示例代碼:
希望對你有幫助!!
這篇關(guān)于如何在 Mongoose 排序結(jié)束時(shí)保持空值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!