GithubHelp home page GithubHelp logo

rishikeshdhokare / mongo-shell-like-query Goto Github PK

View Code? Open in Web Editor NEW

This project forked from equalexperts/mongo-shell-like-query

1.0 2.0 0.0 409 KB

mongo-shell-like-query

License: Apache License 2.0

Scala 100.00%

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

Contributors

anujadongoankar avatar leenaboraee avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.