How to perform an aggregate mongo-query using json in a String?

前端 未结 1 1023
借酒劲吻你
借酒劲吻你 2021-01-17 04:38

I have a string with an aggregate json query (loaded from a file) for mongodb. In robomongo, it works well. So in robomongo, I have:

db.getCollection(\'Odds\         


        
相关标签:
1条回答
  • 2021-01-17 05:09

    You are not far from the solution:

    The aggregate function takes: .aggregate(List<DBObject>) But the JSON.parse you want to use lets you typecast into it, if you have a list in your query, so no Problem

    String query="[....}";
    List<DBObject> q= (List<DBObject>)JSON.parse(query);
    Iterable<DBObject> result=new MongoClient().getDatabase("db").getCollection("coll").aggregate(q).results();`
    

    The results can than be iterated.

    0 讨论(0)
提交回复
热议问题