簡體   English   中英

為什么我不能使用$ match與MongoDB聚合?

[英]Why can't I aggregate with MongoDB with a $match?

我的代碼是:

db.essays.aggregate({
  $match: {
    essayId: 3
  },
  $group: {
    _id: {
      year: {
        $year: '$essayTime'
      },
      month: {
        $month: '$essayTime'
      },
      day: {
        $dayOfMonth: '$essayTime'
      }
    },
    count: {
      $sum: 1
    }
  }
});

它返回一個錯誤: "exception: A pipeline stage specification object must contain exactly one field."

但是,如果我在沒有$match情況下執行此操作,則會按預期返回。 我究竟做錯了什么?

將每個管道階段放入數組中自己的對象:

db.essays.aggregate([
  { $match: {
    essayId: 3
  }},
  { $group: {
    _id: {
      year: {
        $year: '$essayTime'
      },
      month: {
        $month: '$essayTime'
      },
      day: {
        $dayOfMonth: '$essayTime'
      }
    },
    count: {
      $sum: 1
    }
  }}
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM