简体   繁体   English

Java MongoDB无法使用集合中的字符串条件进行查询

[英]Java MongoDB cannot query with String criteria in collection

I have a collection in DB and can get it to a DBCollection just fine. 我在数据库中有一个集合,可以将其添加到DBCollection中。 But when I try to query it with a String field, it doesn't return any result. 但是,当我尝试使用String字段查询它时,它不会返回任何结果。

However, when I query with an Integer field, that query returns results for the same dataset. 但是,当我使用Integer字段查询时,该查询将返回同一数据集的结果。

Here are my trials: 这是我的审判:

// 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();

This is the collection returned from DB: 这是从数据库返回的集合:

{ "_id" : { "$oid" : "574c780aa8268280d1b53162"} , "id" : 1 , "name" : "\"Some name\"" , "city" : "\"Ankara\"" , "country" : "\"Turkey\""}

I suspect those String representations with "\\" characters but couldn't find any workaround to it either. 我怀疑那些带有“ \\”字符的字符串表示形式,但也找不到任何解决方法。

I've checked dozens of articles and also MongoDB documentation but there seems to be nothing special about this. 我检查了许多文章以及MongoDB文档,但似乎没有什么特别之处。 But interestingly it doesn't work. 但是有趣的是,它不起作用。

Hope you can show me a way to get around this. 希望你能告诉我解决这个问题的方法。 Thank you. 谢谢。

Your string values are enclosed in quotes. 您的字符串值用引号引起来。 If you're not able to change that, you can include the quotes in your query: 如果您无法更改它,则可以在查询中包括引号:

allQuery.put("iata", "\"BOS\"");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM