title: Mongo C++ driver is able to bypass document structure rules (the one about keys not starting with $)
body: <p>If I try to insert a document in MongoDB with some key starting with <code>$</code> I get an error message:</p>
  
  <pre><code>&gt; db.x.insert({"a": {"$b": "1"}})
  2016-09-29T21:14:23.078+0200 E QUERY    [thread1] Error: field names cannot start with $ [$b] :
  ...
  </code></pre>
  
  <p>(I have observer a similar behaviour using Node.js driver)</p>
  
  <p>However, running the following C++ program:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include "mongo/client/dbclient.h" // for the driver
  
  // compile with: g++ test.cpp -pthread -lmongoclient -lboost_thread -lboost_system -lboost_regex -o test
  
  void run() {
    mongo::DBClientConnection c;
    c.connect("localhost");
    mongo::BSONObj doc = BSON("a" &lt;&lt; BSON("$b" &lt;&lt; 1));
    c.insert("test.x", doc);
  }
  
  int main() {
      mongo::client::initialize();
      try {
          run();
      } catch( const mongo::DBException &amp;e ) {
          std::cout &lt;&lt; "caught " &lt;&lt; e.what() &lt;&lt; std::endl;
      }
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>I'm able to insert it, as <code>find()</code> shows:</p>
  
  <pre><code>&gt; db.x.find()
  { "_id" : ObjectId("57ed67fdbf3a716e16f6d102"), "a" : { "$b" : 1 } }
  </code></pre>
  
  <p>Thus, it seems that C++ driver is able to "bypass" the document structure rules described <a href="https://docs.mongodb.com/manual/core/document/#field-names" rel="nofollow">in MongoDB documentation</a>. Is there any explanation for this behaviour? It could "break" MongoDB database in some way (I guess that that limitation is for a good reason and having documents in the DB not honoring it could be problematic)</p>
  
  <p>I have observed that this happens only when the key with <code>$</code> is not at first level. For example, if I use</p>
  
  <pre><code>mongo::BSONObj doc = BSON("$b" &lt;&lt; 1);
  </code></pre>
  
  <p>then I get a consistent error</p>
  
  <pre><code>caught OperationException: { index: 0, code: 2, errmsg: "Document can't have $ prefixed field names: $b", op: { _id: ObjectId('57ed6a015365c193cbbb3231'), $b: 1 } }
  </code></pre>
  
  <p>Just in case it is needed, I'm using MongoDB 3.2.0 and legacy C++ driver 1.0.7</p>