Mongo Query - returns documents if data is 0 or null

I have data in the following format:

{ "__v" : 0, "_id" : ObjectId("12367687"), "xyzId" : "ADV_ID", "date" : ISODate("2013-08-19T10:58:21.473Z"), "gId" : "987654", "type" : "checks" } 

For the above data, I have to build a graph for daily, weekly, monthly and yearly data using the created_on field and counting for the type field. I have the following query that works partially.

  db.trackads.aggregate( {$match : {"gId" : "987654",type : 'checks'}}, { $group : { _id : { "day" : {"$week" : "$date"}},cnt : {"$sum" : 1}}}); 

Result:

 { "result" : [ { "_id" : { "day" : 34 }, "cnt" : 734 }, { "_id" : { "day" : 33 }, "cnt" : 349 } ], "ok" : 1 } 

But with the query above I don’t get the results of dates (no week) when the number for "type" = hits is 0. How do I change the mongo query to get the results for count 0 ???

+4
source share
2 answers

If I understand the question correctly, you can do it as follows.

 db.trackads.aggregate({$match:{"groupId":"520a077c62d4b3b00e008905",$or:[{type:{$exists:false}},{type:'impressions'}]}},{$group:{_id:{"day":{"$week":"$created_on"}},cnt:{"$sum":1}}}); 
0
source

The easiest option is to add zeros to the code that uses the results of this query.

MongoDB cannot invent data that is not there. For example, how far back and forth in time do you expect it to start creating zeros?

0
source

Source: https://habr.com/ru/post/1499385/


All Articles