1

I've looked around quite a bit concerning this error, it seems that Mongo won't accept a . or a $ in an update, yet I still get this error

{ [MongoError: not okForStorage]
  name: 'MongoError',
  err: 'not okForStorage',
  code: 12527,
  n: 0,
  connectionId: 18,
  ok: 1 }

This is the object I'm updating:

{
status: "open",
type: "item",
parentId: "4fa13ba2d327ca052d000003",
_id: "4fa13bd6d327ca052d000012",
properties: {
  titleInfo: [
   { title: "some item" }
  ]
  }
}

And I'm updating it to:

{
fedoraId: 'aFedoraLib:438',
status: "closed",
type: "item",
parentId: "4fa13ba2d327ca052d000003",
_id: "4fa13bd6d327ca052d000012",
properties: {
  titleInfo: [
   { title: "some item" }
  ]
  }
}
QuirijnGB
  • 811
  • 1
  • 9
  • 18

3 Answers3

12

Another possible cause I just ran into: storing an object which has periods in the string keys.

MSpreij
  • 1,142
  • 13
  • 20
  • Experiencing the same problem for a key with period. Any idea to solve this? – Jay Kumar Mar 29 '14 at 09:56
  • 2
    MongoDB does not allow `.` or `$` in field names - they have special semantic meaning. See http://docs.mongodb.org/manual/reference/limits/#naming-restrictions – Jesse Fulton Apr 09 '14 at 22:34
  • I ran into the same problem with EJSON `$date` keys that I didn't convert back to normal JSON. – Wes Johnson May 10 '14 at 11:32
8

So for people getting the same error: It's due to the fact that I was including the _id, which Mongo doesn't like apparently

QuirijnGB
  • 811
  • 1
  • 9
  • 18
  • 2
    Note: Embedded objects can have `_id` field. Looks like this error could appear for several reasons. here's another: http://stackoverflow.com/questions/6041109/mongo-update-query-given-error –  Jun 11 '13 at 08:56
5

I ran into this error when trying to save a JSON structure with this key-value pair (coming straight out of an AngularJS app):

 "$$hashKey":"021"

Removing just that key fixed the problem. For others using Angular, it looks like calling Angular's built-in angular.toJson client-side eliminates the $$hashkey keys. From their forums:

$scope.ngObjFixHack = function(ngObj) {
    var output;

    output = angular.toJson(ngObj);
    output = angular.fromJson(output);

    return output;
}
Ben Gotow
  • 14,805
  • 3
  • 42
  • 47