{"id":9951,"date":"2019-05-10T17:31:56","date_gmt":"2019-05-10T15:31:56","guid":{"rendered":"https:\/\/pi3g.com\/?p=9951"},"modified":"2019-05-10T17:31:56","modified_gmt":"2019-05-10T15:31:56","slug":"mongo_orm-exception-missing-bson-key-publish_acl-exception","status":"publish","type":"post","link":"https:\/\/pi3g.com\/de\/mongo_orm-exception-missing-bson-key-publish_acl-exception\/","title":{"rendered":"mongo_orm Exception: fehlender bson-Schl\u00fcssel: publish_acl (Exception)"},"content":{"rendered":"<p>When you extend your field definitions in the mongo_orm document classes, specifically with embedded documents, you might run into the following runtime (!) error:<\/p>\n<blockquote>\n<p>Exception: missing bson key: &lt;name&gt; (Exception)<\/p>\n<\/blockquote>\n<p>This is not an error in your code \u2013 your code is raising, because the database structure is not as it expects it to be!<\/p>\n<h3>Here\u2019s the sample code which I am using for creating a Mongo Auth &amp; ACL DB for VerneMQ:<\/h3>\n<blockquote>\n<p>class PublishACL &lt; Mongo::ORM::EmbeddedDocument<br \/>&nbsp;&nbsp;&nbsp;&nbsp; field pattern : String<br \/>&nbsp;&nbsp;&nbsp;&nbsp; field max_qos : Int32<br \/>&nbsp;&nbsp;&nbsp;&nbsp; field allowed_retain : Bool<br \/>&nbsp;&nbsp;&nbsp;&nbsp; field max_payload_size : Int32<br \/>\nend<\/p>\n<p>class SubscribeACL &lt; Mongo::ORM::EmbeddedDocument<br \/>&nbsp;&nbsp;&nbsp;&nbsp; field pattern : String<br \/>&nbsp;&nbsp;&nbsp;&nbsp; field max_qos : Int32<br \/>\nend<\/p>\n<p>\nclass MQTTCredentials &lt; Mongo::ORM::Document<br \/>&nbsp;&nbsp;&nbsp;&nbsp; collection_name &#8220;vmq_acl_auth&#8221;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; field mountpoint : String<br \/>&nbsp;&nbsp;&nbsp;&nbsp; field client_id : String<br \/>&nbsp;&nbsp;&nbsp;&nbsp; field username : String<br \/>&nbsp;&nbsp;&nbsp;&nbsp; field passhash : String<br \/>&nbsp;&nbsp;&nbsp;&nbsp; embeds_many :publish_acl, class_name: Papi::PublishACL&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp; embeds_many :subscribe_acl, class_name: Papi::SubscribeACL<\/p>\n<p>\n<\/p>\n<p># keys are unique per apikey_prefix<br \/>\ndef self.getcredentials(apikey_prefix : String, bcm_serial : String)<br \/>&nbsp;&nbsp;&nbsp;&nbsp; success = false<br \/>&nbsp;&nbsp;&nbsp;&nbsp; #if the credentials don&#8217;t exist yet, they are added<br \/>&nbsp;&nbsp;&nbsp;&nbsp; creds = self.first({&#8220;client_id&#8221; =&gt; bcm_serial, &#8220;username&#8221; =&gt; apikey_prefix})<br \/>&nbsp;&nbsp;&nbsp;&nbsp; if !creds<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; creds = self.new()<br \/>&nbsp;&nbsp;&nbsp;&nbsp; end<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; (snip)<\/p>\n<p>&nbsp;&nbsp;&nbsp; publish_acl = PublishACL.new()<br \/>&nbsp;&nbsp;&nbsp;&nbsp; subscribe_acl = SubscribeACL.new()<br \/>&nbsp;&nbsp;&nbsp;&nbsp; # <a href=\"https:\/\/vernemq.com\/docs\/configuration\/db-auth.html\">https:\/\/vernemq.com\/docs\/configuration\/db-auth.html<\/a><br \/>&nbsp;&nbsp;&nbsp;&nbsp; publish_acl.pattern = &#8220;#&#8221;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; publish_acl.max_qos = 2&nbsp; # Int<br \/>&nbsp;&nbsp;&nbsp;&nbsp; publish_acl.allowed_retain = true<\/p>\n<p>&nbsp;&nbsp;&nbsp; subscribe_acl.pattern = &#8220;#&#8221; <br \/>&nbsp;&nbsp;&nbsp;&nbsp; subscribe_acl.max_qos = 2<br \/>&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp; creds.publish_acl &lt;&lt;&nbsp; publish_acl <br \/>&nbsp;&nbsp;&nbsp;&nbsp; creds.subscribe_acl &lt;&lt; subscribe_acl&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp; success = creds.save<\/p>\n<p>(snip)<\/p>\n<p>end<\/p>\n<p>end<\/p>\n<\/blockquote>\n<p>The ACLs were inserted after I already had database entries without them:<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2019\/05\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"993\" height=\"169\" title=\"image\" style=\"display: inline; background-image: none;\" alt=\"image\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2019\/05\/image_thumb.png\" border=\"0\"><\/a><\/p>\n<p><strong>MongoORM error message:<\/strong><\/p>\n<blockquote>\n<p>Exception: missing bson key: publish_acl (Exception)<br \/>&nbsp;&nbsp; from src\/db.cr:0:4 in &#8216;from_bson&#8217;<br \/>&nbsp;&nbsp; from lib\/mongo_orm\/src\/mongo_orm\/querying.cr:93:7 in &#8216;all&#8217;<br \/>&nbsp;&nbsp; from lib\/mongo_orm\/src\/mongo_orm\/querying.cr:90:3 in &#8216;all&#8217;\n<\/p>\n<p>&nbsp; (snip)<\/p>\n<p>&nbsp; from \/usr\/share\/crystal\/src\/fiber.cr:47:34 in &#8216;-&gt;&#8217;<br \/>&nbsp;&nbsp; from ???<\/p>\n<p><\/p>\n<\/blockquote>\n<p>MongoORM does not find the expected fields publish_acl and subscribe_acl, which were added later.<\/p>\n<p>Solution: drop (or upgrade) the documents in the Collection, which do not conform to your new standard.<\/p>\n<p>This is, for instance, how a database entry looks like for the above:<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2019\/05\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"993\" height=\"342\" title=\"image\" style=\"display: inline; background-image: none;\" alt=\"image\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2019\/05\/image_thumb-1.png\" border=\"0\"><\/a><\/p>\n<blockquote>\n<p>\n<\/p>\n<\/blockquote>\n<h2>Bonus<\/h2>\n<blockquote>\n<p>in lib\/mongo_orm\/src\/mongo_orm\/extended_bson.cr:40: no overload matches &#8216;BSON#[]=&#8217; with types String, Array(Papi::PublishACL)<\/p>\n<p>Overloads are:<\/p>\n<p>&#8211; BSON#[]=(key, value : Int32)<\/p>\n<p>&#8211; BSON#[]=(key, value : Int64)<\/p>\n<p>&#8211; BSON#[]=(key, value : Binary)<\/p>\n<p>&#8211; BSON#[]=(key, value : Bool)<\/p>\n<p>&#8211; BSON#[]=(key, value : Float64 | Float32)<\/p>\n<p>&#8211; BSON#[]=(key, value : MinKey)<\/p>\n<p>&#8211; BSON#[]=(key, value : MaxKey)<\/p>\n<p>&#8211; BSON#[]=(key, value : Nil)<\/p>\n<p>&#8211; BSON#[]=(key, value : ObjectId)<\/p>\n<p>&#8211; BSON#[]=(key, value : String)<\/p>\n<p>&#8211; BSON#[]=(key, value : Symbol)<\/p>\n<p>&#8211; BSON#[]=(key, value : Time)<\/p>\n<p>&#8211; BSON#[]=(key, value : Timestamp)<\/p>\n<p>&#8211; BSON#[]=(key, value : Code)<\/p>\n<p>&#8211; BSON#[]=(key, value : BSON)<\/p>\n<p>&#8211; BSON#[]=(key, value : Regex)<\/p>\n<p>&#8211; BSON#[]=(key, value : Mongo::ORM::EmbeddedDocument)<\/p>\n<p>bson[_key] = value<\/p>\n<\/blockquote>\n<p>You <strong>cannot<\/strong> do this (currently) with MongoORM:<\/p>\n<blockquote>\n<p>creds.publish_acl = [ publish_acl ]<\/p>\n<\/blockquote>\n<p>instead you have to use: <\/p>\n<blockquote>\n<p>creds.publish_acl &lt;&lt;&nbsp; publish_acl <\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Wenn Sie Ihre Felddefinitionen in den mongo_orm-Dokumentenklassen erweitern, insbesondere mit eingebetteten Dokumenten, k\u00f6nnen Sie auf den folgenden Laufzeitfehler (!) sto\u00dfen: Exception: missing bson key:  (Exception) Dies ist kein Fehler in Ihrem Code - Ihr Code wird ausgel\u00f6st, weil die Datenbankstruktur nicht so ist, wie er sie erwartet!...<\/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":[477,534,531,589],"class_list":["post-9951","post","type-post","status-publish","format-standard","hentry","category-crystal-language","tag-crystal-lang","tag-mongo_orm","tag-mongodb","tag-vernemq"],"_links":{"self":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/9951","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=9951"}],"version-history":[{"count":1,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/9951\/revisions"}],"predecessor-version":[{"id":9952,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/9951\/revisions\/9952"}],"wp:attachment":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/media?parent=9951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/categories?post=9951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/tags?post=9951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}