Coder Social home page Coder Social logo

mongo-shell-like-query's Introduction

                                          mongo-shell-like-query
                                          ======================


                            Executing mongo-shell-like-query with mongo Java driver

Describe your read queries the same as you do on the mongo client shell, and the library runs it, for both regular queries as well as use of the Mongo aggregation facility

Download a jar for the current release (v1.0) here

#Example


    String query = ”db.users.find( { ‘name’ : ‘John’} )”;

    MongoQueryParser parser = new MongoQueryParser();
    MongoQuery mongoQuery = parser.parse(query, new HashMap());
    BasicDBList results = mongoQuery.execute(mongoDB);

#Features

  1. It supports all kinds of read queries (both find as well as aggregation) using various operators

  2. It supports query chaining (chain the use of sort, skip, and limit in any order)

  3. It tolerates whitespace as: ”db.users.find( { ‘name’ : ‘John’} )” is of course, the same as ”db . users . find ( { ‘name’ : ‘John’ } )”

  4. It supports parameterized queries, wherein values can be specified at runtime


       String query = ”db.users.find( { ‘name’ : ‘givenName’} )”
       Map[String,String]  params= new HashMap()
       map.add(“givenName”, “John”)

       MongoQueryParser parser = new MongoQueryParser();
       MongoQuery mongoQuery = parser.parse(query, params);
       BaiscDBList results = mongoQuery.execute(mongoDB);
  1. It supports parameterized queries with strongly typed data (Most BSON types are supported)

       String query = ”db.users.find( { salary : ‘sal#Long’} )” 
       Map[String,String]  params= new HashMap()
       map.add(“sal”, “5000”)

       MongoQueryParser parser = new MongoQueryParser();
       MongoQuery mongoQuery = parser.parse(query, params);
       BaiscDBList results = mongoQuery.execute(mongoDB);

Query looks like mongo query string. Providing placeholders is very simple and have consistent pattern. ParameterName#DataType (supported datatypes are String|Boolean|Integer|Double|ObjectId)

For more examples checkout ‘src/test/resources’ folder and MongoQueryIntegrationTest

  1. It supports both scala and java api

#Usage

  1. Download the .jar file and add it to your project

2.Use library:

a) Java API


  String query = "db.users.find( { 'role' : 'manager'} )";
  DB mongoDB = new MongoClient().getDB("your-db-name");

  MongoQuery mongoQuery = new   MongoQueryParser().parse(query, new HashMap());
  BasicDBList results = mongoQuery.execute(mongoDB);
  System.out.println(results.size());

b) Scala API


  val query = "db.users.find( { 'role' : 'manager'} )"
  val mongoConn = MongoConnection()
  val mongoDB = mongoConn("your-db-name")
  
  val mongoQuery: MongoQuery = parser.parse(query, Map())
  val results = mongoQuery.execute(mongoDB)
  println(results.size)

END-OF-FILE

mongo-shell-like-query's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mongo-shell-like-query's Issues

Please provide an example using Aggregation Query

I've been trying to use this library to perform an aggregation query but I'm having difficulty doing so. Could you provide an example of how to execute an aggregation query using $match & $projection in java?

Allow aggregate method with opt

            MongoQueryParser parser = new MongoQueryParser();
            MongoQuery mongoQuery = parser.parse(longQuery, new HashMap());
            BasicDBList dbObjList = mongoQuery.execute(db);

Query is an aggregation: db.events.aggregate([....])

I have an exception: Caused by: com.mongodb.MongoCommandException: Command failed with error 16945: 'Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in.

When i try to pass it like: db.events.aggregate([....],{ allowDiskUse: true }) then i have an exception:
java.lang.RuntimeException: invalid query
at com.ee.dynamicmongoquery.MongoQueryParser.parse(MongoQueryParser.scala:31)
at com.ee.dynamicmongoquery.MongoQueryParser.parse(MongoQueryParser.scala:27)
......
Caused by: java.lang.ClassCastException: null

How can i pass allowDiskUse:true to query?

Map / Reduce support?

Would it be possible to add map/reduce to the supported command sets.

I'd really like to be able to spam all the map/reduce commands in a single string.

m = function () { emit(this.field, 1);}; r = function (k, vals) {return Array.sum(vals);}; res = db.mycollection.mapReduce(m, r, { out : "temp_col1" }); db.temp_col1.find({value: {$gt: 1}}); db.temp_col1.drop();

It would be ok if the format weren't the same as the mongo shell, but required some special flags to define functions.

findOne() support

This is a pretty minor/low priority request, but is it any effort to add findOne() to the allowable queries. It's one we run from the normal shell all the time. I assume I can do the same with limit, but this is meant to be shell-like-queries.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.