Executing Mongo like Query (JSON)through Java

后端 未结 5 1631
执笔经年
执笔经年 2020-12-25 14:54

I was wondering if there is a way to execute mongo like query directly through Java i.e. we give mongoDB like query as a String to a function in Java driver for mongoDB as a

5条回答
  •  没有蜡笔的小新
    2020-12-25 15:36

    What you showed here is not JSON, it's Javascript code for embedded MongoDB Shell. If you need for some reason to execute the code inside Java environment you will have to embed Javascript engine (like Rhino) and implement compatible API.

    Otherwise you just need to convert JSON to DBObject and you can do this with JSON.parse() method or any other JSON-mapping library like Jackson. Note that MongoDB uses extended set of data types that are not present in JSON: http://www.mongodb.org/display/DOCS/Data+Types+and+Conventions

    UPD: Scott Hernandez pointed out about JSON.parse.

提交回复
热议问题