簡體   English   中英

mongodb總用戶按日期計數

[英]mongodb aggregate users count by date

我正在嘗試按某個日期對所有用戶進行分組,我想要的是月份的名稱和用戶數,月份將是一個變量,因此可以更改為年或周。 這是我的代碼:

logs = user_collection.aggregate([
        # Lets find our records
        {"$match" => {"zip_code" => {"$exists" => true}, "dob" => {"$exists" => true}, "firstname" => {"$exists" => true}, "lastname" => {"$exists" => true}, "email" => {"$exists" => true}}}, 

        # Now lets group on the name counting how many grouped documents we have
        {"$group" => {"date" => { "$#{period_type}" => "$created" }, "count" => {"$sum" => "1" }}} 
      ]) 

在我的情況下,period_type是month。 好像我做錯了,因為我有這個錯誤

Mongo::OperationFailure: Database command 'aggregate' failed: (errmsg: 'exception: unknown group operator '$month''; code: '15952'; ok: '0.0').

這里有兩個問題。 首先, $group運算符按您指定為_id分組,此處未提供。 其次,所有字段( _id除外)都必須遵循<field1>: { <accumulator1> : <expression1> } 結構

嘗試用您的分組電話代替:

{"$group" => {"_id" => { "$#{period_type}" => "$created" }, "count" => {"$sum" => "1" }}}     

暫無
暫無

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

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