{"id":9346,"date":"2019-04-17T20:38:02","date_gmt":"2019-04-17T18:38:02","guid":{"rendered":"https:\/\/pi3g.com\/?p=9346"},"modified":"2019-04-17T20:38:02","modified_gmt":"2019-04-17T18:38:02","slug":"mongo_orm-custom-name-for-collection","status":"publish","type":"post","link":"https:\/\/pi3g.com\/de\/mongo_orm-custom-name-for-collection\/","title":{"rendered":"mongo_orm benutzerdefinierter Name f\u00fcr die Sammlung"},"content":{"rendered":"<p>mongo_orm will automatically determine the name for your collection by using the Module and Class name, and appending an \u201cs\u201d.<\/p>\n<p>In cases where you want to name your collection, you can simply do this:<\/p>\n<blockquote>\n<p>class PapiTest &lt; Mongo::ORM::Document<br \/>&nbsp;&nbsp;&nbsp;&nbsp; <strong>collection_name &#8220;Buster&#8221;<\/strong><br \/>&nbsp;&nbsp;&nbsp;&nbsp; field test : String<br \/>\nend<\/p>\n<p>get &#8220;\/&#8221; do<br \/>&nbsp;&nbsp;&nbsp;&nbsp; pt = PapiTest.new<br \/>&nbsp;&nbsp;&nbsp;&nbsp; pt.test = &#8220;I want your sex&#8221;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; pt.save!<br \/>&nbsp;&nbsp; &#8220;Hello World!&#8221;<br \/>\nend<\/p>\n<p>Kemal.run<\/p>\n<\/blockquote>\n<p>In my example, the collection will be called <strong>Buster<\/strong>. <\/p>\n<p>Internally, this is realized by a macro which looks like this:<\/p>\n<blockquote>\n<p># specify the collection name to use otherwise it will use the model&#8217;s name<\/p>\n<p>macro collection_name(name)<\/p>\n<p>&nbsp;&nbsp;&nbsp; {% SETTINGS[:collection_name] = name.id %}<\/p>\n<p>end<\/p>\n<\/blockquote>\n<p>It\u2019s important to understand, that the development is done in the context of a macro here! Therefore .id is NOT a class variable\/ method you have to pass in, but a special function in the macro context which will return a MacroID for the string\u2019s content.<\/p>\n<p>Refer to this documentation: <a href=\"https:\/\/crystal-lang.org\/api\/0.27.2\/Crystal\/Macros\/MacroId.html\">https:\/\/crystal-lang.org\/api\/0.27.2\/Crystal\/Macros\/MacroId.html<\/a><\/p>\n<p>belongs_to is realized in a similar fashion. It allows you to create relationships between Documents \/ Collections.<\/p>\n<blockquote>\n<p>belongs_to :user, class_name: FictitiousModule::FictiticiousClass<\/p>\n<\/blockquote>\n<p>is realized internally as a macro:<\/p>\n<blockquote>\n<p>macro belongs_to(model_name, class_name=nil)<\/p>\n<p>&nbsp; field {{class_name ? class_name.id.underscore.gsub(\/::\/,&#8221;_&#8221;) : model_name.id}}_id : BSON::ObjectId <\/p>\n<p>&nbsp; (\u2026)<\/p>\n<\/blockquote>\n<p>here class_name: in the original macro call refers to the second variable which is nil by default. The macro checks whether it is not nil, and chooses what to use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mongo_orm bestimmt automatisch den Namen f\u00fcr Ihre Sammlung, indem es den Modul- und Klassennamen verwendet und ein \"s\" anh\u00e4ngt. In F\u00e4llen, in denen Sie Ihre Sammlung benennen m\u00f6chten, k\u00f6nnen Sie einfach Folgendes tun: class PapiTest &lt; Mongo::ORM::Document collection_name &quot;Buster&quot; field test : String end get &quot;\/&quot; do pt = PapiTest.new pt.test = &quot;Ich m\u00f6chte...<\/p>","protected":false},"author":830,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[416],"tags":[570,409,477,568,494,569,534,531,571],"class_list":["post-9346","post","type-post","status-publish","format-standard","hentry","category-crystal-language","tag-collection-name","tag-crystal","tag-crystal-lang","tag-custom-name-for-collection","tag-macro","tag-mongo-driver","tag-mongo_orm","tag-mongodb","tag-set-collection-name"],"_links":{"self":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/9346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/users\/830"}],"replies":[{"embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/comments?post=9346"}],"version-history":[{"count":1,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/9346\/revisions"}],"predecessor-version":[{"id":9347,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/9346\/revisions\/9347"}],"wp:attachment":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/media?parent=9346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/categories?post=9346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/tags?post=9346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}