1

I want to put String "http://1.1.1.1/olala" into mongodb, but instead of

{data: "http://1.1.1.1/olala"}

I get

{data: {"http://1": {1: {1: "1/olala"}}}}

Is there a standard way to insert string with special characters into mongodb using java?

The way I'm doing the upsert is

collection.updateOne(new Document("name", key), 
                     new Document("$inc", new Document(methodName, 1)), 
                     opt.upsert(true), null);

where methodName is url with commas. I tried to quote values, but instead got

{data: {"'http://1": {1: {1: "1/olala'"}}}}

And beside that, I suggest that there would be more "bad" characters later.

UPD more complex example, but don't understand how it will help

final UpdateOptions opt = new UpdateOptions();
final String methodName = "http://1.1.1.1/ololo";
final MongoCollection<Document> collection =
    MongoAsyncClientFactory.getCollection();
//final String key = Statistics.getMainKey();
final String key = "any-key-you-want";
collection.updateOne(new Document("name", key),
                     new Document("$inc", new Document(methodName, 1)),
                     opt.upsert(true), null);
Ivan Ivanov
  • 2,076
  • 16
  • 33
  • 2
    Which part of the code you've shown is the URL? It would really help if you could provide a short but complete example. – Jon Skeet Jun 23 '15 at 07:36

1 Answers1

0

According to MongoDB --> You are allowed to use use any (UTF8) character in the field name which aren't special (contains ".", or starts with "$")

and field "http://1.1.1.1/olala" contains . which is special character in MongoDB and refers to a subdocument, so it reads it as,

 {"http://1": 
    {1: 
       {1: 
          "1/olala" : someValue
       }
    }
 }

Refer to issue tracker https://jira.mongodb.org/browse/SERVER-3229

This issue has been addressed as Works as Designed, so I don't think that there will be any update regarding this.

UPDATE

I have just noticed that update code in the question doesnot actually seem to achieve {data: "http://1.1.1.1/olala"} or even {name: "http://1.1.1.1/olala"}

collection.updateOne(new Document("name", key), 
                     new Document("$inc", new Document(methodName, 1)), 
                     opt.upsert(true), null);

This actually creates document with a {"name" : "test" } for key = "test" and then updates the document to

{
  "http://1" : {"1" : { "1" : { "1/ololo" : 1 }
    }
  },
  "name" : "test"
}

So, if this is not something you are trying to achieve , consider changing your logic.

Harshil
  • 883
  • 1
  • 8
  • 25
  • as I understand, solution is to manually escape `$` and `.`, or to avoid them at all? – Ivan Ivanov Jun 23 '15 at 07:55
  • You will have to avoid them Escaping `.` and `$` is not possible .You may try replacing them with some other combination of characters etc. – Harshil Jun 23 '15 at 08:38
  • Also, I have just updated the answer. May be you need to check your update method too – Harshil Jun 23 '15 at 08:47
  • But what if I _need_ to store special chars? Use \u escape instead? About update method - yes, It creates object with name `key` if it does not exist. It is logging, and name of `key` changes every day. – Ivan Ivanov Jun 23 '15 at 08:55
  • You cannot store `.` and keys starting with `$` in mongoDB ,directly ,by any means. Your application's data access layer can although create a mechanism that will encode and decode every such occurrences to some other group of characters – Harshil Jun 23 '15 at 09:04
  • I understand. Just tried to find a way to do that with help of MongoDriver, and not DAO. – Ivan Ivanov Jun 23 '15 at 10:47