{"id":1006,"date":"2023-05-17T07:31:40","date_gmt":"2023-05-17T07:31:40","guid":{"rendered":"https:\/\/mustafauzun.co\/blog\/?p=1006"},"modified":"2023-05-17T07:31:40","modified_gmt":"2023-05-17T07:31:40","slug":"short-mongodb-queries","status":"publish","type":"post","link":"https:\/\/mustafauzun.co\/blog\/short-mongodb-queries\/","title":{"rendered":"Short MongoDB Queries"},"content":{"rendered":"\n<p>To sort a table in reverse order, you can use the &#8220;-1&#8221; number in your query.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>db.getCollection(&#39;Table&#39;) .find({}).sort({_id: -1})<\/code><\/pre><\/div>\n\n\n\n<p>If you want to find a value from within a property.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>db.getCollection(&#39;Table&#39;).find({&#39;myRow.otherRowinmyRow&#39;: &#39;Hello World&#39;})<\/code><\/pre><\/div>\n\n\n\n<p>To quickly access the logs of a specific account code.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>var account = db.getCollection(&#39;Account&#39;).findOne({code: &#39;admin&#39;})\ndb.getCollection(&#39;Table&#39;).find({accountId: account._id}).sort({_id: -1})<\/code><\/pre><\/div>\n\n\n\n<p>Provide a list of an account&#8217;s entries between two dates.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>var account = db.getCollection(&#39;Account&#39;).findOne({code: &#39;admin&#39;})\ndb.getCollection(&#39;Table&#39;).find({&quot;accountId&quot;: account._id, &quot;CreateDate&quot;:{$gte:ISODate(&quot;2023-05-05&quot;),$lt:ISODate(&quot;2023-08-25&quot;)}})<\/code><\/pre><\/div>\n\n\n\n<p>Provide the number of documents in an array between two dates.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>var account = db.getCollection(&#39;Account&#39;).findOne({code: &#39;admin&#39;})\ndb.getCollection(&#39;Table&#39;).aggregate([\n   { &quot;$match&quot;: { &quot;accountId&quot;: account._id, &quot;createDate&quot;:{$gte:ISODate(&quot;2023-05-05&quot;),$lt:ISODate(&quot;2023-08-25&quot;)} } },\n   { &quot;$group&quot;: {\n       &quot;_id&quot;: null,\n        &quot;count&quot;: {\n            &quot;$sum&quot;: { &quot;$size&quot;: &quot;$Documents&quot; }\n        }\n    } }\n])<\/code><\/pre><\/div>\n\n\n\n<p>Find by verification code within a document array.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>db.getCollection(&#39;Table&#39;).find({Documents: { $elemMatch : { &#39;code&#39; : &#39;111&#39;}}})<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>To sort a table in reverse order, you can use the &#8220;-1&#8221; number in your query. If you want to find a value from within<\/p>\n","protected":false},"author":1,"featured_media":1007,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-1006","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/1006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/comments?post=1006"}],"version-history":[{"count":1,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/1006\/revisions"}],"predecessor-version":[{"id":1008,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/1006\/revisions\/1008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media\/1007"}],"wp:attachment":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media?parent=1006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/categories?post=1006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/tags?post=1006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}