【发布时间】:2015-09-08 21:06:50
【问题描述】:
我想将字符串“http://1.1.1.1/olala”放入 mongodb,而不是
{data: "http://1.1.1.1/olala"}
我明白了
{data: {"http://1": {1: {1: "1/olala"}}}}
有没有一种标准的方法可以使用 java 将带有特殊字符的字符串插入到 mongodb 中?
我做 upsert 的方式是
collection.updateOne(new Document("name", key),
new Document("$inc", new Document(methodName, 1)),
opt.upsert(true), null);
methodName 是带逗号的网址。我试图引用值,但得到了
{data: {"'http://1": {1: {1: "1/olala'"}}}}
除此之外,我建议以后会有更多的“坏”角色。
UPD 更复杂的示例,但不明白它有什么帮助
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);
【问题讨论】:
-
您显示的代码的哪一部分是 URL?如果您能提供一个简短但完整的示例,那将非常有帮助。
标签: java mongodb escaping special-characters insertion