{"action":"created","comment":{"url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/comments/71923613","id":71923613,"diff_hunk":"@@ -0,0 +1,166 @@\n++++\n+next = \"/2-models/allowed-types\"\n+prev = \"/2-models\"\n+title = \"Introduction\"\n+toc = true\n+weight = 1\n+\n++++\n+\n+A **model** is the fundamental way you access data in MongoDB with Mangrove. Models lets you treat C++ classes as objects you can store in the database. When you make a class a model, it is given the semantics of a MongoDB collection, and has access to all of these static methods (TODO: add doxygen link).\n+\n+## Creating a Model\n+\n+You can turn any *default-constructible* C++ class into a model by having it inherit publicly from the `mangrove::model<T>` base class, where `T` is type of the class you are making a model.\n+\n+```cpp\n+// Example of inheritance required for model\n+class Message : mangrove::model<Message> {\n+    bsoncxx::oid author_id;\n+    std::string content;\n+    std::chrono::system_clock::time_point time_sent;\n+}\n+```\n+\n+This inheritance gives the inheriting class access to the interface specified here (TODO: Doxygen link). The inheritance also gives you access to the `_id` variable. Since every document stored in a MongoDB database contains an `_id`, the model stores one automatically for you. By default this of type {{% a_blank \"`bsoncxx::oid`\" \"http://mongodb.github.io/mongo-cxx-driver/classbsoncxx_1_1oid.html\" %}}, but you can customize it. We'll discuss how to do this in a [later section](/2-models/custom_id).\n+\n+## Specifying Fields to Serialize\n+\n+Before you can use a model, you must specify which of its fields you want to be included when an instance of a model is serialized and saved to the database. You can do this with the `MANGROVE_MAKE_KEYS_MODEL` macro.\n+\n+This macro accepts the class you are registering as a model, as well as a variadic list of the fields you want to include, wrapped as name-value pairs (**NVPs**).\n+\n+```cpp\n+// Example of registering fields in model\n+class Message : mangrove::model<Message> {\n+    bsoncxx::oid author_id;\n+    std::string content;\n+    std::chrono::system_clock::time_point time_sent;\n+    int dont_include_me;\n+\n+    MANGROVE_MAKE_KEYS_MODEL(Message,\n+                             MANGROVE_NVP(author_id),\n+                             MANGROVE_NVP(content),\n+                             MANGROVE_NVP(time_sent))\n+}\n+```\n+\n+The `MANGROVE_NVP` macro specifies that you want its argument serialized in such a way that its **name** or **key** is the name of the variable. For example, in the above code sample, the resulting saved BSON document would look something like the following:\n+\n+```json\n+{\n+    \"_id\" : ObjectId(\"576d7238e249391db3271e31\"),\n+    \"author_id\" : ObjectId(\"576d5145e2493917691144f1\"),\n+    \"content\" : \"Hey, did you see how cool Mangrove is?!\",\n+    \"time_sent\" : ISODate(\"2016-07-01T14:42:06.018Z\")\n+}\n+```\n+\n+{{% notice info %}}\n+While there is no limitation to the complexity of the classes you can turn into a model, there are limitations regarding which types are serializable and can be included in the `MANGROVE_MAKE_KEYS_MODEL` macro. These limitations are discussed in the [next section](/2-models/allowed-types). \n+{{% /notice %}}\n+\n+If you don't want the name of the field to be exactly the same as the name of your variable, you can use the macro `MANGROVE_CUSTOM_NVP` (TODO: may be different) to specify a custom name for the resulting BSON element of a particular field.","path":"hugo/content/2-models/introduction.md","position":63,"original_position":63,"commit_id":"7b5c943b8ef8752c621338a0e7e524e8556eafd3","original_commit_id":"7b5c943b8ef8752c621338a0e7e524e8556eafd3","user":{"login":"Machyne","id":2737181,"avatar_url":"https://avatars.githubusercontent.com/u/2737181?v=3","gravatar_id":"","url":"https://api.github.com/users/Machyne","html_url":"https://github.com/Machyne","followers_url":"https://api.github.com/users/Machyne/followers","following_url":"https://api.github.com/users/Machyne/following{/other_user}","gists_url":"https://api.github.com/users/Machyne/gists{/gist_id}","starred_url":"https://api.github.com/users/Machyne/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Machyne/subscriptions","organizations_url":"https://api.github.com/users/Machyne/orgs","repos_url":"https://api.github.com/users/Machyne/repos","events_url":"https://api.github.com/users/Machyne/events{/privacy}","received_events_url":"https://api.github.com/users/Machyne/received_events","type":"User","site_admin":false},"body":"Is there a ticket for this TODO? If not, please file one.","created_at":"2016-07-22T18:25:46Z","updated_at":"2016-07-22T18:25:46Z","html_url":"https://github.com/mongodb/mongo-cxx-odm/pull/15#discussion_r71923613","pull_request_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/15","_links":{"self":{"href":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/comments/71923613"},"html":{"href":"https://github.com/mongodb/mongo-cxx-odm/pull/15#discussion_r71923613"},"pull_request":{"href":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/15"}}},"pull_request":{"url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/15","id":78340436,"html_url":"https://github.com/mongodb/mongo-cxx-odm/pull/15","diff_url":"https://github.com/mongodb/mongo-cxx-odm/pull/15.diff","patch_url":"https://github.com/mongodb/mongo-cxx-odm/pull/15.patch","issue_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/issues/15","number":15,"state":"open","locked":false,"title":"CXXODM-46 CXXODM-48 CXXODM-14 Add README, Doxyfile, and Hugo-powered static docs","user":{"login":"adamchel","id":1976694,"avatar_url":"https://avatars.githubusercontent.com/u/1976694?v=3","gravatar_id":"","url":"https://api.github.com/users/adamchel","html_url":"https://github.com/adamchel","followers_url":"https://api.github.com/users/adamchel/followers","following_url":"https://api.github.com/users/adamchel/following{/other_user}","gists_url":"https://api.github.com/users/adamchel/gists{/gist_id}","starred_url":"https://api.github.com/users/adamchel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamchel/subscriptions","organizations_url":"https://api.github.com/users/adamchel/orgs","repos_url":"https://api.github.com/users/adamchel/repos","events_url":"https://api.github.com/users/adamchel/events{/privacy}","received_events_url":"https://api.github.com/users/adamchel/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-07-21T15:47:39Z","updated_at":"2016-07-22T18:25:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4f5a0340e27c087f654c61433e19a00fe540fa9c","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/15/commits","review_comments_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/15/comments","review_comment_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/issues/15/comments","statuses_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/statuses/7b5c943b8ef8752c621338a0e7e524e8556eafd3","head":{"label":"mongodb:doxygen","ref":"doxygen","sha":"7b5c943b8ef8752c621338a0e7e524e8556eafd3","user":{"login":"mongodb","id":45120,"avatar_url":"https://avatars.githubusercontent.com/u/45120?v=3","gravatar_id":"","url":"https://api.github.com/users/mongodb","html_url":"https://github.com/mongodb","followers_url":"https://api.github.com/users/mongodb/followers","following_url":"https://api.github.com/users/mongodb/following{/other_user}","gists_url":"https://api.github.com/users/mongodb/gists{/gist_id}","starred_url":"https://api.github.com/users/mongodb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mongodb/subscriptions","organizations_url":"https://api.github.com/users/mongodb/orgs","repos_url":"https://api.github.com/users/mongodb/repos","events_url":"https://api.github.com/users/mongodb/events{/privacy}","received_events_url":"https://api.github.com/users/mongodb/received_events","type":"Organization","site_admin":false},"repo":{"id":60376659,"name":"mongo-cxx-odm","full_name":"mongodb/mongo-cxx-odm","owner":{"login":"mongodb","id":45120,"avatar_url":"https://avatars.githubusercontent.com/u/45120?v=3","gravatar_id":"","url":"https://api.github.com/users/mongodb","html_url":"https://github.com/mongodb","followers_url":"https://api.github.com/users/mongodb/followers","following_url":"https://api.github.com/users/mongodb/following{/other_user}","gists_url":"https://api.github.com/users/mongodb/gists{/gist_id}","starred_url":"https://api.github.com/users/mongodb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mongodb/subscriptions","organizations_url":"https://api.github.com/users/mongodb/orgs","repos_url":"https://api.github.com/users/mongodb/repos","events_url":"https://api.github.com/users/mongodb/events{/privacy}","received_events_url":"https://api.github.com/users/mongodb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mongodb/mongo-cxx-odm","description":"","fork":false,"url":"https://api.github.com/repos/mongodb/mongo-cxx-odm","forks_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/forks","keys_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/teams","hooks_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/hooks","issue_events_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/issues/events{/number}","events_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/events","assignees_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/assignees{/user}","branches_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/branches{/branch}","tags_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/tags","blobs_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/git/refs{/sha}","trees_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/statuses/{sha}","languages_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/languages","stargazers_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/stargazers","contributors_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/contributors","subscribers_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/subscribers","subscription_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/subscription","commits_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/commits{/sha}","git_commits_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/git/commits{/sha}","comments_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/comments{/number}","issue_comment_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/issues/comments{/number}","contents_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/contents/{+path}","compare_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/merges","archive_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/downloads","issues_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/issues{/number}","pulls_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls{/number}","milestones_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/milestones{/number}","notifications_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/labels{/name}","releases_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/releases{/id}","deployments_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/deployments","created_at":"2016-06-03T20:45:42Z","updated_at":"2016-07-13T13:37:49Z","pushed_at":"2016-07-22T18:03:29Z","git_url":"git://github.com/mongodb/mongo-cxx-odm.git","ssh_url":"git@github.com:mongodb/mongo-cxx-odm.git","clone_url":"https://github.com/mongodb/mongo-cxx-odm.git","svn_url":"https://github.com/mongodb/mongo-cxx-odm","homepage":null,"size":2669,"stargazers_count":1,"watchers_count":1,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":1,"default_branch":"master"}},"base":{"label":"mongodb:master","ref":"master","sha":"e887d7729f2163c01e09091f7a4859e786394d02","user":{"login":"mongodb","id":45120,"avatar_url":"https://avatars.githubusercontent.com/u/45120?v=3","gravatar_id":"","url":"https://api.github.com/users/mongodb","html_url":"https://github.com/mongodb","followers_url":"https://api.github.com/users/mongodb/followers","following_url":"https://api.github.com/users/mongodb/following{/other_user}","gists_url":"https://api.github.com/users/mongodb/gists{/gist_id}","starred_url":"https://api.github.com/users/mongodb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mongodb/subscriptions","organizations_url":"https://api.github.com/users/mongodb/orgs","repos_url":"https://api.github.com/users/mongodb/repos","events_url":"https://api.github.com/users/mongodb/events{/privacy}","received_events_url":"https://api.github.com/users/mongodb/received_events","type":"Organization","site_admin":false},"repo":{"id":60376659,"name":"mongo-cxx-odm","full_name":"mongodb/mongo-cxx-odm","owner":{"login":"mongodb","id":45120,"avatar_url":"https://avatars.githubusercontent.com/u/45120?v=3","gravatar_id":"","url":"https://api.github.com/users/mongodb","html_url":"https://github.com/mongodb","followers_url":"https://api.github.com/users/mongodb/followers","following_url":"https://api.github.com/users/mongodb/following{/other_user}","gists_url":"https://api.github.com/users/mongodb/gists{/gist_id}","starred_url":"https://api.github.com/users/mongodb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mongodb/subscriptions","organizations_url":"https://api.github.com/users/mongodb/orgs","repos_url":"https://api.github.com/users/mongodb/repos","events_url":"https://api.github.com/users/mongodb/events{/privacy}","received_events_url":"https://api.github.com/users/mongodb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mongodb/mongo-cxx-odm","description":"","fork":false,"url":"https://api.github.com/repos/mongodb/mongo-cxx-odm","forks_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/forks","keys_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/teams","hooks_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/hooks","issue_events_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/issues/events{/number}","events_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/events","assignees_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/assignees{/user}","branches_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/branches{/branch}","tags_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/tags","blobs_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/git/refs{/sha}","trees_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/statuses/{sha}","languages_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/languages","stargazers_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/stargazers","contributors_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/contributors","subscribers_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/subscribers","subscription_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/subscription","commits_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/commits{/sha}","git_commits_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/git/commits{/sha}","comments_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/comments{/number}","issue_comment_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/issues/comments{/number}","contents_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/contents/{+path}","compare_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/merges","archive_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/downloads","issues_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/issues{/number}","pulls_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls{/number}","milestones_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/milestones{/number}","notifications_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/labels{/name}","releases_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/releases{/id}","deployments_url":"https://api.github.com/repos/mongodb/mongo-cxx-odm/deployments","created_at":"2016-06-03T20:45:42Z","updated_at":"2016-07-13T13:37:49Z","pushed_at":"2016-07-22T18:03:29Z","git_url":"git://github.com/mongodb/mongo-cxx-odm.git","ssh_url":"git@github.com:mongodb/mongo-cxx-odm.git","clone_url":"https://github.com/mongodb/mongo-cxx-odm.git","svn_url":"https://github.com/mongodb/mongo-cxx-odm","homepage":null,"size":2669,"stargazers_count":1,"watchers_count":1,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/15"},"html":{"href":"https://github.com/mongodb/mongo-cxx-odm/pull/15"},"issue":{"href":"https://api.github.com/repos/mongodb/mongo-cxx-odm/issues/15"},"comments":{"href":"https://api.github.com/repos/mongodb/mongo-cxx-odm/issues/15/comments"},"review_comments":{"href":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/15/comments"},"review_comment":{"href":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mongodb/mongo-cxx-odm/pulls/15/commits"},"statuses":{"href":"https://api.github.com/repos/mongodb/mongo-cxx-odm/statuses/7b5c943b8ef8752c621338a0e7e524e8556eafd3"}}}}
{"actor":{"display_login":"Machyne"}}