我正在使用 com.mongodb.util.JSON
和 parse
方法来查询 mongodb 数据库。
我在特定查询中遇到问题,我想选择要显示的特定字段。
mongoshell 查询:
{{type:'考试'},{score:1,_id:0}}
在 Java 中:
String query="{{type:'exam'},{score:1,_id:0}}";
DBObject dbObject=(DBObject) JSON.parse(query);
List<DBObject> dbOList=coll.find(dbObject).toArray();
查询应仅显示score
字段。
这将返回一个空的dbOList。
我认为 JSON.parse
无法解析指定要显示字段的查询。这是真的吗?
我如何解析这种查询?
我正在开发的应用程序是100%动态的,用户可以设置他们想要的查询,我需要处理所有可能的情况!
我没有 map ,这就是我不使用 JONGO 的原因。
我想要解析的 JSON 可能比这个示例更复杂,JSON.parse
非常适合解析嵌套 JSON。但是,我还需要处理特定的字段查询。
我是否缺少使用 mongo-java-driver v2.12.2
中的 JSON.parse
的内容?
编辑:
{"_id" : { "$oid" : "50906d7fa3c412bb040eb577" }, "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb578" }, "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb579" }, "student_id" : 0, "type" : "homework", "score" : 14.8504576811645 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57a" }, "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57b" }, "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57c" }, "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57d" }, "student_id" : 1, "type" : "homework", "score" : 21.33260810416115 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57e" }, "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57f" }, "student_id" : 2, "type" : "exam", "score" : 19.88180838833524 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb580" }, "student_id" : 2, "type" : "quiz", "score" : 1.528220212203968 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb581" }, "student_id" : 2, "type" : "homework", "score" : 60.9750047106029 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb582" }, "student_id" : 2, "type" : "homework", "score" : 97.75889721343528 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb583" }, "student_id" : 3, "type" : "exam", "score" : 92.6244233936537 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb584" }, "student_id" : 3, "type" : "quiz", "score" : 82.59760859306996 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb585" }, "student_id" : 3, "type" : "homework", "score" : 50.81577033538815 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb586" }, "student_id" : 3, "type" : "homework", "score" : 92.71871597581605 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb587" }, "student_id" : 4, "type" : "exam", "score" : 87.89071881934647 }
我尝试在JAVA中使用的mongoshell查询:
db.grade.find({type:exam},{score:1,_id:0})
此查询仅按照我的需要在 shell 中显示 score
字段。
问题是当我使用 JSON.parse
在 JAVA 中执行此操作时。
伊斯梅尔
最佳答案
请了解 mongo 查询是如何工作的,
就mongodb而言,查询模式和投影模式是这样的,
collection.find(query,projection); --> grade.find({type:exam,_id:0},{score:1});
很明显,您需要将查询和投影部分分开。因此,请按照以下方式按预期执行查询,
String json = "{'type':'exam','student_id':0}";
DBObject dbObj = (DBObject) JSON.parse(json);
DBObject projection = (DBObject) JSON.parse("{'score':1,}");
System.out.println(dbObj);
Dao dao = new Dao();
final DBCollection collection = dao.getDb("test").getCollection("test");
**List<DBObject> dbOList=collection.find(dbObj,projection).toArray();**
System.out.println(dbOList);
这会为您提供预期的结果,因此您肯定需要像上面那样将查询和投影部分分开。
您将获得单独分数字段列表的结果(默认为 ID)
[{ "_id" : { "$oid" : "53d8574e26b7d8cafbf80f9b"} , "score" : 54.6535436362647}]
感谢和问候, 哈里
关于java - 解析 JSON 并指定投影时的 com.mongodb.util.JSON.parse 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25014993/