0

是否可以在我的目标键上使用 substr 来区分 runCommand ?我不断得到missing : after property id :

db.runCommand(
   {
       distinct: "mycollection",
       key: {"myfield" : { $substr: { "$myfield", 0, 10 } }},
   }
)
4

1 回答 1

0

不能用runCommand distinct. 您需要使用 agg 框架来处理字段,然后使用 获取不同的值$group,因此:

db.foo.aggregate([
{$group: {_id: {$substr: [ "$myfield",0,10]} }}
  ]);

获取这些不同值的计数通常很有用:

db.foo.aggregate([
{$group: {_id: {$substr: ["$myfield",0,10]}, count: {$sum:1} }}
  ]);

于 2020-11-13T17:12:59.637 回答