Example usage for com.mongodb.util JSON parse

List of usage examples for com.mongodb.util JSON parse

Introduction

In this page you can find the example usage for com.mongodb.util JSON parse.

Prototype

public static Object parse(final String s, final BSONCallback c) 

Source Link

Document

Parses a JSON string and constructs a corresponding Java object by calling the methods of a org.bson.BSONCallback BSONCallback during parsing.

Usage

From source file:com.mingo.query.aggregation.PipelineBuilder.java

License:Apache License

/**
 * Builds find criteria for plain query. Is thread safe.
 *
 * @param json plain query : {field1:value1, field2:value2, ...}
 *             if value is null or empty then condition
 *             for filed with that value will be removed from criteria.
 *             Example:/*from   w ww .  j  a v  a  2 s  .  c o  m*/
 *             source: { field1 : "value1", field2:""}
 *             after build: { field1 : "value1" }
 * @return find criteria
 */
public DBObject build(String json) {
    lock.lock();
    try {
        return (DBObject) JSON.parse(json, bsonCallback);
    } finally {
        lock.unlock();
    }
}

From source file:com.mingo.query.aggregation.PipelineBuilder.java

License:Apache License

/**
 * Builds operators pipeline for aggregation query. Is thread safe.
 * Conditions with empty values will be removed from pipeline.
 * Example://  ww  w  . j av a 2  s .  co  m
 * source: {$match : { "field1": { $in: []}, "field2": { "$gt" : "value2"}}}
 * after build:[{$match : {field2": { "$gt" : "value2"}}}]
 * <p/>
 * source: {$match : { "field1": { $in: [value1, value2]}, "field2": { "$gt" : ""}}}
 * after build:[ {$match : { "field1": { $in: [value1, value2]}}} ]
 *
 * @param json query
 * @return operators
 */
public BasicDBList buildAggregation(String json) {
    lock.lock();
    try {
        BasicDBList operators = (BasicDBList) JSON.parse(wrap(json), bsonCallback);
        return operators.isEmpty() ? (BasicDBList) JSON.parse(wrap(DEFAULT_QUERY)) : operators;
    } finally {
        lock.unlock();
    }
}

From source file:me.yyam.mongodbutils.MongoDbOperater.java

public void insert(String dbName, String colName, String json) {
    DB db = mongoClient.getDB(dbName);// w  w w  . j  av a 2 s . com
    DBCollection coll = db.getCollection(colName);
    BasicBSONObject obj = (BasicBSONObject) JSON.parse(json, new DefaultDBCallback(coll));
    BasicDBObject record = new BasicDBObject(obj);
    coll.insert(record);
}

From source file:me.yyam.mongodbutils.MongoDbOperater.java

public void update(String dbName, String colName, String findJson, String json) {
    DB db = mongoClient.getDB(dbName);//  ww  w .  j  av  a  2 s. c  o m
    DBCollection coll = db.getCollection(colName);
    BasicBSONObject locateObj = (BasicBSONObject) JSON.parse(findJson, new DefaultDBCallback(coll));
    BasicDBObject locate = new BasicDBObject(locateObj);
    BasicBSONObject obj = (BasicBSONObject) JSON.parse(json, new DefaultDBCallback(coll));
    BasicDBObject record = new BasicDBObject(obj);
    coll.update(locate, record, false, true);
}

From source file:me.yyam.mongodbutils.MongoDbOperater.java

public static void main(String[] args) throws Exception {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB db = mongoClient.getDB("test");
    DBCollection coll = db.getCollection("rztest");
    String sql = "select * from table1 where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3') order by age,name limit 123";
    MongoDbOperater ope = new MongoDbOperater();
    ope.setMongoClient(mongoClient);//w  w w. j ava  2 s  .  co m
    QueryInfo info = ope.sql2QueryInfo("abc", sql);
    System.out.println(info.debugStr());

    sql = "select * from algoflow_instance_log where functionName='f1' and reuseResult=false and returnCode=0 and action='LEAVE' order by timestamp desc limit 100";
    info = ope.sql2QueryInfo("abc", sql);
    System.out.println(info.debugStr());

    String updateSql = "update table1 set a=1, b='sdaf', c='2012-11-23 17:45:32' where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3')";
    QueryInfo uinfo = ope.sql2QueryInfo("abc", updateSql);
    System.out.println(uinfo.debugStr());

    String json = "{\"a\":123,\"b\":345}";
    BasicBSONObject obj = (BasicBSONObject) JSON.parse(json, new DefaultDBCallback(coll));
    BasicDBObject dbObj = new BasicDBObject(obj);
    System.out.println(dbObj);
}

From source file:me.yyam.mongodbutils.QueryInfo.java

public void setQuery(String json) {
    query = (DBObject) JSON.parse(json, new BasicBSONCallback());
    System.out.println(query);
}

From source file:me.yyam.mongodbutils.QueryInfo.java

public void setSort(String json) {
    order = (DBObject) JSON.parse(json, new BasicBSONCallback());
}