Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-40070

ReplaceOne: The dollar ($) prefixed field is not valid for storage

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.0.6
    • Component/s: None
    • Labels:
      None
    • Query Optimization
    • v4.0, v3.6
    • Query 2019-04-22

      With MongoDB 3.6, dollar-prefixed fields were allowed. Invoking ReplaceOne on such a document, however, results in an error:

      rs0:PRIMARY> db.coll.insertOne({"a": {"$foo": 1}})
      {
              "acknowledged" : true,
              "insertedId" : ObjectId("5c86835900b48b0b86ee197d")
      }
      rs0:PRIMARY> doc = db.coll.findOne()
      { "_id" : ObjectId("5c86835900b48b0b86ee197d"), "a" : { "$foo" : 1 } }
      rs0:PRIMARY> doc
      { "_id" : ObjectId("5c86835900b48b0b86ee197d"), "a" : { "$foo" : 1 } }
      rs0:PRIMARY> doc['b'] = 2
      2
      rs0:PRIMARY> db.coll.replaceOne({}, doc)
      2019-03-11T11:49:14.749-0400 E QUERY    [js] WriteError: The dollar ($) prefixed field '$foo' in 'a.$foo' is not valid for storage. :
      WriteError({
              "index" : 0,
              "code" : 52,
              "errmsg" : "The dollar ($) prefixed field '$foo' in 'a.$foo' is not valid for storage.",
              "op" : {
                      "q" : {                },
                      "u" : {
                              "_id" : ObjectId("5c86835900b48b0b86ee197d"),
                              "a" : {
                                      "$foo" : 1
                              },
                              "b" : 2
                      },
                      "multi" : false,
                      "upsert" : false
              }
      })
      WriteError@src/mongo/shell/bulk_api.js:461:48
      Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:841:49
      Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13
      Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21
      DBCollection.prototype.replaceOne@src/mongo/shell/crud_api.js:489:17
      @(shell):1:1

      This happened on MongoDB 4.0.5, but probably happens on all versions. This smells of a validation that should have been removed in 3.6 when these field names were allowed.

      I can't think of any reason that documents valid for insertOne wouldn't be valid for replaceOne.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            jason.coombs@yougov.com Jason R. Coombs
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated: