【问题标题】:MongoDB is it possible to runCommand distinct with substr on keyMongoDB是否可以在键上使用 substr 来运行不同的命令
【发布时间】:2021-02-25 15:10:34
【问题描述】:

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

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

【问题讨论】:

  • 清晰性:您想获得 myfield 的不同值,然后 然后 执行子字符串或先执行子字符串,然后从中获取不同的值?
  • @BuzzMoschetti 我想要区分子字符串

标签: mongodb substring distinct substr runcommand


【解决方案1】:

无法使用 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} }}
  ]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 2021-05-15
    • 2018-09-28
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多