Java MongoDB 无法使用集合中的字符串条件进行查询
Java MongoDB cannot query with String criteria in collection
我在 DB 中有一个集合,可以将其添加到 DBCollection 中。
但是当我尝试使用 String 字段查询它时,它没有 return 任何结果。
但是,当我使用 Integer 字段进行查询时,该查询 returns 结果是同一数据集。
这是我的试验:
// It doesn't work this way, cursor has nothing
DBObject allQuery = new BasicDBObject();
allQuery.put("data", "someName");
List cursor = collection.find(allQuery).toArray();
// This time it works, only change is that I query with an Integer value now
DBObject allQuery = new BasicDBObject();
allQuery.put("xyz", 1);
List cursor = collection.find(allQuery).toArray();
这是从数据库return编辑的集合:
{ "_id" : { "$oid" : "574c780aa8268280d1b53162"} , "id" : 1 , "name" : "\"Some name\"" , "city" : "\"Ankara\"" , "country" : "\"Turkey\""}
我怀疑那些带有“\”字符的字符串表示,但也找不到任何解决方法。
我检查了数十篇文章和 MongoDB 文档,但似乎没有什么特别之处。但有趣的是它不起作用。
希望你能告诉我解决这个问题的方法。
谢谢。
您的字符串值用引号引起来。如果您无法更改它,您可以在查询中包含引号:
allQuery.put("iata", "\"BOS\"");
我在 DB 中有一个集合,可以将其添加到 DBCollection 中。 但是当我尝试使用 String 字段查询它时,它没有 return 任何结果。
但是,当我使用 Integer 字段进行查询时,该查询 returns 结果是同一数据集。
这是我的试验:
// It doesn't work this way, cursor has nothing
DBObject allQuery = new BasicDBObject();
allQuery.put("data", "someName");
List cursor = collection.find(allQuery).toArray();
// This time it works, only change is that I query with an Integer value now
DBObject allQuery = new BasicDBObject();
allQuery.put("xyz", 1);
List cursor = collection.find(allQuery).toArray();
这是从数据库return编辑的集合:
{ "_id" : { "$oid" : "574c780aa8268280d1b53162"} , "id" : 1 , "name" : "\"Some name\"" , "city" : "\"Ankara\"" , "country" : "\"Turkey\""}
我怀疑那些带有“\”字符的字符串表示,但也找不到任何解决方法。
我检查了数十篇文章和 MongoDB 文档,但似乎没有什么特别之处。但有趣的是它不起作用。
希望你能告诉我解决这个问题的方法。 谢谢。
您的字符串值用引号引起来。如果您无法更改它,您可以在查询中包含引号:
allQuery.put("iata", "\"BOS\"");