{"id":409,"date":"2018-08-21T07:46:29","date_gmt":"2018-08-21T05:46:29","guid":{"rendered":"https:\/\/kszyh.kisim.eu.org\/?p=409"},"modified":"2018-08-21T07:48:40","modified_gmt":"2018-08-21T05:48:40","slug":"count-documents-using-aggregation","status":"publish","type":"post","link":"https:\/\/kszyh.kisim.eu.org\/?p=409","title":{"rendered":"Count documents using Aggregation"},"content":{"rendered":"<p>Counts log entries,\u00a0 with type equal 'CREATE&#8217; , fetch patients data\u00a0 from another collection,\u00a0 remove empty documents (if patients was not found the field is empty array), count by provided user id.<\/p>\n<pre class=\"wrap:true lang:js decode:true\">var user_id = '5ad070640c9faddd41a4b665'\r\n\r\ndb.getCollection('log').aggregate([\r\n{ $match: { message: {$eq: 'CREATE' } }},\r\n{\r\n $lookup: {\r\n     from: 'patients',\r\n     localField: 'meta.patient',\r\n     foreignField: '_id',\r\n     as: 'patient'\r\n     }   \r\n},\r\n{ $match: { patient: {$not: {$size: 0}} }},\r\n{ $match: {'meta.user': {$eq: ObjectId(user_id)}}},\r\n    {\r\n      $count: \"created\"\r\n    }\r\n])<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Counts log entries,\u00a0 with type equal 'CREATE&#8217; , fetch patients data\u00a0 from another collection,\u00a0 remove empty documents (if patients was not found the field is empty array), count by provided user id. var user_id = '5ad070640c9faddd41a4b665&#8242; db.getCollection(&#8217;log&#8217;).aggregate([ { $match: { message: {$eq: 'CREATE&#8217; } }}, { $lookup: { from: 'patients&#8217;,&#8230; <a class=\"continue-reading-link\" href=\"https:\/\/kszyh.kisim.eu.org\/?p=409\"> Continue reading <span class=\"meta-nav\">&rarr; <\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97,19],"tags":[110],"class_list":["post-409","post","type-post","status-publish","format-standard","hentry","category-mongodb","category-skrypty","tag-aggregation"],"_links":{"self":[{"href":"https:\/\/kszyh.kisim.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kszyh.kisim.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kszyh.kisim.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kszyh.kisim.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kszyh.kisim.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=409"}],"version-history":[{"count":3,"href":"https:\/\/kszyh.kisim.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/409\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/kszyh.kisim.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/409\/revisions\/412"}],"wp:attachment":[{"href":"https:\/\/kszyh.kisim.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kszyh.kisim.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kszyh.kisim.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}