node.js - MongoDB 独特函数和字母排序

标签 node.js mongodb sorting distinct

我有一个关于对 mongodb 中不同命令的结果进行排序的简单问题。

我在 NodeJS 应用程序中使用 Monk 从我的简单数据库中获取类别:

db.getCollection('citazioni').distinct('category')

但是我如何应用排序功能,就像我在找到具有以下内容的文档时成功做到的那样:

collection.find({}, {sort: {'_id': -1}} ??

谢谢!

最佳答案

Monk 支持底层 Node.js 原生驱动 distinct()方法及其 signature

Collection.prototype.distinct = function (field, query, fn) { ... }

从该提交中可以看出,它通过所选集合对象上的 .col 访问器实现了 Node native 驱动程序集合类型的独特方法:

this.col.distinct(field, query, promise.fulfill);

因此您可以将其与原生 JavaScript 一起实现 sort() 方法如下:

// Perform a distinct query against the category field
db.getCollection('citazioni').distinct('category', function(err, categories) {
    console.log(categories.sort());
}

关于node.js - MongoDB 独特函数和字母排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31122977/

相关文章:

javascript - Nodemailer 包括通过数组迭代表

javascript - useEffect 不监听 localStorage

c++ - 使用示例代码测试 mongocxx c++ 驱动程序

java - Java vector 中的冒泡排序

java - 如何对二维数组进行排序?

windows - node npm windows 文件路径太长无法安装包

node.js - GitLab 依赖扫描需要源代码中的 package-lock.json 才能执行

node.js - 在 Mongoose 中填充 + 聚合

node.js - Mongoose 模式字段在 MongoDB 中是反向排序的

jquery - DataTables 对仅包含字形的单元格进行排序