MongoDB 按字段返回所有文档而不重复?

3 mongodb node.js mongo-shell

在 mongo shell 中,有什么方法可以返回集合中的所有文档,但按特定字段对它们进行排序,并删除字段与其他文档相同的任何文档?

谢谢。

Cha*_*les 6

对于排序我会使用sort。要删除具有相同值的文档:distict

就像是:

db.collection.find().sort({field : 1}).distinct('field')
Run Code Online (Sandbox Code Playgroud)

  • 我收到“错误:排序不能与 unique 一起使用” (6认同)