MongoDB按字段返回所有文档,没有重复吗?

问题描述 投票:1回答:2

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

谢谢。

node.js mongodb mongo-shell
2个回答
0
投票

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

类似:

db.collection.find().sort({field : 1}).distinct('field')

0
投票

您可以合计使用组。

db.usersProject.aggregate([


{
   $group:
    {
      _id: { projectId: "$projectId" },
      dups: { $addToSet: "$_id" },
      count: { $sum:1 }
   }
 },
 {
   $match:
     {
       count: {"$gt": 1}
     }
 }
]);

请关注this文章以供参考

© www.soinside.com 2019 - 2024. All rights reserved.