1

I had updated the mongoDB using $setOnInsert. It is working in 2.4 version of mongoDB but it is throwing error in 2.6 version.

Following are the code

   BasicDBObject update = new BasicDBObject();
   BasicDBObject visits = new BasicDBObject();
   visits.append("request", getRequestInfo(input));//Some documents
   update.append("$setOnInsert", {}).append("$push", visits);
   collection.update(searchQuery, update, true, false);

Error

  java.lang.RuntimeException: com.mongodb.MongoException: '$setOnInsert' is empty. You must specify a field like so: {$mod: {: ...}}

Note: I had two mongoDB servers. It is working in one server. It is giving above issue in another. Only difference is version.

After Updating the document in 2.4.8

   {
     "_id" : ObjectId("536113766a47069648b4695d"),
     "request" : [
            {
              "currentVisit" : "1398254762",
              "lastVisit" : "1398254762"
            }
      ],
     "session" : "1",
     "uuid" : "113862726056042"
    }

Any problem with the version.

Any suggestion will be grateful.

9
  • Very odd. So what exactly was happening before the upgrade. Can you please add that to your question? Commented May 1, 2014 at 11:35
  • 2.4 allowed for empty operator values? Sounds like a bug that was secretly fixed, has happened before Commented May 1, 2014 at 11:37
  • Agreed. Seems like the application had a bug that was ignored by MongoDB until now. Commented May 1, 2014 at 11:38
  • @Sammaye Yes. 2.4 allows empty document in $setOnInsert. Commented May 1, 2014 at 11:39
  • Sounds buggy. Is the only change the server version, or have you changed driver as well? Commented May 1, 2014 at 11:40

2 Answers 2

2

This is the issue you are relating to: https://jira.mongodb.org/browse/SERVER-12266 which applies since 2.5.4.

It seems this behaviour is unlikely to change due to this issue being marked as "works as designed".

The last comment being:

I spoke to Scott Hernandez about this today, and he explained the new strictness around empty modifiers is intended to alert users that were inadvertently sending over empty updates.

Instead you should have your code to put:

append("$setOnInsert", {}).

Into an if statement to detect its setting.

Sign up to request clarification or add additional context in comments.

Comments

2

So the syntax here is well, intriguing at best. But I'd call it an edge case and not really how you should have been invoking this. The better version would be:

   BasicDBObject update = new BasicDBObject();
   BasicDBObject visits = new BasicDBObject();
   visits.append("request", getRequestInfo(input));//Some documents
   update.append("$setOnInsert", new BasicDBObject("$push", visits));
   collection.update(searchQuery, update, true, false);

Which I'm pretty sure is not going to cause an error.

P.S : A very interesting structure to have this as an array. So you can comment if there was a reason, but it would seemingly be a case that $setOnInsert may not actually be required and you could just possibly leave the standard $push operation in the update, that is unless you really mean to only ever have one array element there.

1 Comment

Thanks. I am getting the following error java.lang.RuntimeException: com.mongodb.MongoException: The dollar ($) prefixed field '$push' in '$push' is not valid for storage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.