{"id":2660,"date":"2022-08-30T15:26:43","date_gmt":"2022-08-30T15:26:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/getting-all-database-changes-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:43","modified_gmt":"2022-08-30T15:26:43","slug":"getting-all-database-changes-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/getting-all-database-changes-collection-of-common-programming-errors\/","title":{"rendered":"Getting all database changes-Collection of common programming errors"},"content":{"rendered":"<p>There are several approaches you could use to track changes to your data, depending on whether you are just interested in monitoring current activity or need to create some sort of transactional history.<\/p>\n<p>In rough order of least-to-most effort:<\/p>\n<h3>1) Enable the MongoDB Query Profiler<\/h3>\n<p>If you enable the query profiler at a level of &#8220;<strong>2<\/strong>&#8221; it will collect profiling data for all operations (reads as well as writes) to a specific database. You can also enable this in your configuration options) to change the profiling default for all databases on <code>mongod<\/code> startup. The profile data is saved in a capped collection per database and will only contain a recent snapshot of queries. Query profiling status can also be changed at runtime, so you can easily enable or disable as required.<\/p>\n<h3>2) Add Mongoid callbacks to your application<\/h3>\n<p>Add appropriate logic Mongoid callbacks such as <code>after_insert<\/code>, <code>after_save<\/code>, <code>after_upsert<\/code> depending on what information you are trying to capture.<\/p>\n<h3>3) Create a tailable cursor on the MongoDB oplog<\/h3>\n<p>If you run MongoDB as part of a replica set (or with the <code>--replSet<\/code> option), it creates a capped collection called the <code>oplog<\/code> (operations log). You can use a tailable cursor to follow changes as they are committed to the <code>oplog<\/code>. The <code>oplog<\/code> details all changes to the database, and is the mechanism MongoDB uses for replication.<\/p>\n<p id=\"rop\"><small>Originally posted 2014-02-04 13:26:58. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>There are several approaches you could use to track changes to your data, depending on whether you are just interested in monitoring current activity or need to create some sort of transactional history. In rough order of least-to-most effort: 1) Enable the MongoDB Query Profiler If you enable the query profiler at a level of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2660","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2660"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2660\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}