List of usage examples for com.mongodb.util JSON serialize
public static String serialize(final Object object)
Serializes an object into its JSON form.
This method delegates serialization to JSONSerializers.getLegacy
From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java
License:Apache License
/** * Returns all the files in a bucket./*from ww w . j ava 2 s. c om*/ * * @param databaseName the database * @param bucketName the bucket * @return the files in the bucket * @throws DatasourceException * @throws NotFoundException */ @Override public List<String> getAll(String databaseName, String bucketName) throws DatasourceException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } if (!bucketExists(databaseName, bucketName)) { throw new NotFoundException("The bucket doesn't exist in the database"); } DB mongoDatabase = mongoClient.getDB(databaseName); GridFS gfsBucket = new GridFS(mongoDatabase, bucketName); DBCursor cursor = gfsBucket.getFileList(); Iterator<DBObject> curIter = cursor.iterator(); List<String> fileList = new ArrayList<>(); while (curIter.hasNext()) { DBObject current = curIter.next(); fileList.add(JSON.serialize(current)); } return fileList; } catch (MongoException ex) { logger.error("An error occured while retrieving file list", ex); throw new DatasourceException("An error occured while retrieving file list"); } }
From source file:org.basex.modules.MongoDB.java
License:BSD License
/** * Convert collection result(DBCursor) into Item {@link Item} element. * @param result DBCursor/*from w w w . j av a 2s . com*/ * @return Item * @throws QueryException */ private Item cursorToItem(final Str handler, final DBCursor cursor) throws QueryException { if (cursor != null) { try { if (cursor.count() == 1) { Iterator<DBObject> row = cursor.iterator(); return objectToItem(handler, row.next()); } else { final Str json = Str.get(JSON.serialize(cursor)); return returnResult(handler, json); } } catch (final Exception ex) { throw MongoDBErrors.generalExceptionError(ex); } } else { return null; } }
From source file:org.basex.modules.MongoDB.java
License:BSD License
/** * Convert collection DBObject into Item {@link Item} element. * @param object DBObject (one row result) * @return Item//from w ww . j a v a 2 s.co m * @throws QueryException */ private Item objectToItem(final Str handler, final DBObject object) throws QueryException { if (object != null) { try { final Str json = Str.get(JSON.serialize(object)); return returnResult(handler, json); } catch (final Exception ex) { throw MongoDBErrors.generalExceptionError(ex); } } else { return null; } }
From source file:org.basex.modules.MongoDB.java
License:BSD License
/** * This method is for aggregating with all pipeline options. All the options * should be given in sequence like: ('$group:{..}',...). * @param handler database handler// w w w .j a va2s . co m * @param col collection name * @param first aggregation compulsary * @param additionalOps other pipeline options in sequence. * @return Item * @throws QueryException */ public Item aggregate(final Str handler, final Item col, final Item first, final Value additionalOps) throws Exception { final DB db = getDbHandler(handler); AggregationOutput agg; DBObject[] pipeline = null; if (additionalOps != null && (!additionalOps.isEmpty())) { if (additionalOps instanceof Map) { pipeline = mapToDBObjectArray((Map) additionalOps); } else { int length = (int) additionalOps.size(); if (length > 0) { pipeline = new BasicDBObject[length]; int i = 0; for (Item x : additionalOps) { pipeline[i++] = getDbObjectFromStr(x); } } else { pipeline = null; } } } db.requestStart(); try { if (additionalOps != null && (!additionalOps.isEmpty())) { agg = db.getCollection(itemToString(col)).aggregate(getDbObjectFromStr(first), pipeline); } else { agg = db.getCollection(itemToString(col)).aggregate(getDbObjectFromStr(first)); } final Iterable<DBObject> d = agg.results(); return returnResult(handler, Str.get(JSON.serialize(d))); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(e.getMessage()); } finally { db.requestDone(); } }
From source file:org.basex.modules.MongoDB.java
License:BSD License
/** * Mongodb Mapreduce function with 3 parameters, Map, reduce and query Option. * @param handler Database Handler./*from w w w .j ava 2 s .c om*/ * @param col Collection name * @param map Map method * @param reduce Reduce Method * @param query Selection options. * @return Items. * @throws Exception */ public Item mapreduce(final Str handler, final Str col, final Str map, final Str reduce, final Item finalalize, final Item query, final Map options) throws Exception { final DB db = getDbHandler(handler); if (map == null) { throw MongoDBErrors.generalExceptionError("Map function cannot be empty in Mapreduce"); } final DBObject q = query != null ? getDbObjectFromStr(query) : null; final DBCollection collection = db.getCollection(itemToString(col)); String out = null; String outType = null; OutputType op = MapReduceCommand.OutputType.INLINE; if (options != null) { for (Item k : options.keys()) { String key = (String) k.toJava(); if (key.equals("outputs")) { out = (String) options.get(k, null).toJava(); } if (key.equals("outputype")) { outType = (String) options.get(k, null).toJava(); } } if (out != null) { if (outType.toUpperCase().equals("REPLACE")) { op = MapReduceCommand.OutputType.REPLACE; } else if (outType.toUpperCase().equals("MERGE")) { op = MapReduceCommand.OutputType.MERGE; } else if (outType.toUpperCase().equals("REDUCE")) { op = MapReduceCommand.OutputType.REDUCE; } } } db.requestStart(); try { MapReduceCommand cmd = new MapReduceCommand(collection, map.toJava(), reduce.toJava(), out, op, q); if (finalalize != null) { cmd.setFinalize((String) finalalize.toJava()); } final MapReduceOutput outcmd = collection.mapReduce(cmd); return returnResult(handler, Str.get(JSON.serialize(outcmd.results()))); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(e); } finally { db.requestDone(); } }
From source file:org.basex.modules.MongoDB.java
License:BSD License
/** * Mapreduce all functions in xquery's Map like :{"map":"function(){..}" * , "reduce":"function(){}"}.// w w w . jav a 2 s.co m * @param handler * @param col collection name * @param options all options of Mapreduce including "map" in key. * @return * @throws Exception */ public Item mapreduce(final Str handler, final Str col, final Map options) throws Exception { if (options == null) { throw MongoDBErrors.generalExceptionError("Map optoins are empty"); } final DB db = getDbHandler(handler); final DBCollection collection = db.getCollection(itemToString(col)); String out = null; String outType = null; String map = null; String reduce = null; DBObject query = null; DBObject sort = null; int limit = 0; String finalalize = null; OutputType op = MapReduceCommand.OutputType.INLINE; for (Item k : options.keys()) { String key = (String) k.toJava(); Value val = options.get(k, null); String value = (String) val.toJava(); if (key.toLowerCase().equals("map")) { map = (String) value; } else if (key.toLowerCase().equals("reduce")) { reduce = value; } else if (key.toLowerCase().equals("outputs")) { out = value; } else if (key.toLowerCase().equals("outputype")) { outType = value; } else if (key.toLowerCase().equals("limit")) { if (val.type().instanceOf(SeqType.ITR_OM)) { long l = ((Item) val).itr(null); limit = (int) l; } else { throw MongoDBErrors.generalExceptionError(" Expected integer Value"); } } else if (key.toLowerCase().equals(SORT)) { sort = getDbObjectFromStr(Str.get(value)); } else if (key.toLowerCase().equals(QUERY)) { query = getDbObjectFromStr(Str.get(value)); } else if (key.toLowerCase().equals(FINALIZE)) { finalalize = value; } } if (out != null && outType != null) { if (outType.toUpperCase().equals("REPLACE")) { op = MapReduceCommand.OutputType.REPLACE; } else if (outType.toUpperCase().equals("MERGE")) { op = MapReduceCommand.OutputType.MERGE; } else if (outType.toUpperCase().equals("REDUCE")) { op = MapReduceCommand.OutputType.REDUCE; } } else if (out != null) { op = MapReduceCommand.OutputType.REPLACE; } if (map == null) { throw MongoDBErrors.generalExceptionError("Map function cannot be empty"); } db.requestStart(); try { MapReduceCommand cmd = new MapReduceCommand(collection, map, reduce, out, op, query); if (finalalize != null) { cmd.setFinalize(finalalize); } if (limit != 0) { cmd.setLimit(limit); } if (sort != null) { cmd.setSort(sort); } final MapReduceOutput outcmd = collection.mapReduce(cmd); return returnResult(handler, Str.get(JSON.serialize(outcmd.results()))); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(e); } finally { db.requestDone(); } }
From source file:org.basex.modules.nosql.MongoDB.java
License:BSD License
/** * Convert collection result(DBCursor) into Item {@link Item} element. * @param handler database handler/*w w w. j av a 2 s .c om*/ * @param cursor DBcursor * @return Item * @throws QueryException query exception */ private Item cursorToItem(final Str handler, final DBCursor cursor) throws QueryException { if (cursor != null) { try { if (cursor.count() == 1) { Iterator<DBObject> row = cursor.iterator(); return objectToItem(handler, row.next()); } final Str json = Str.get(JSON.serialize(cursor)); return returnResult(handler, json); } catch (final Exception ex) { throw MongoDBErrors.generalExceptionError(ex); } } return null; }
From source file:org.basex.modules.nosql.MongoDB.java
License:BSD License
/** * Convert collection List<DBObject> into Item {@link Item} element. * @param handler database handler/* w ww. j a v a 2 s .c om*/ * @param result DBcursor * @return Item * @throws QueryException query exception */ private Item cursorListToItem(final Str handler, final List<DBObject> result) throws QueryException { if (result != null) { try { final Str json = Str.get(JSON.serialize(result)); return returnResult(handler, json); } catch (final Exception ex) { throw MongoDBErrors.generalExceptionError(ex); } } return null; }
From source file:org.basex.modules.nosql.MongoDB.java
License:BSD License
/** * Convert collection DBObject into Item {@link Item} element. * @param handler databse handler//from w w w. ja v a 2s .c o m * @param object DBObject (one row result) * @return Item * @throws QueryException query exception */ private Item objectToItem(final Str handler, final DBObject object) throws QueryException { if (object != null) { try { final Str json = Str.get(JSON.serialize(object)); return returnResult(handler, json); } catch (final Exception ex) { throw MongoDBErrors.generalExceptionError(ex); } } return null; }
From source file:org.basex.modules.nosql.MongoDB.java
License:BSD License
/** * This method is for aggregating with all pipeline options. All the options * should be given in sequence like: ('$group:{..}',...). * @param handler database handler database handler * @param col collection name//from ww w. ja v a 2 s . c om * @param first aggregation compulsary * @param additionalOps other pipeline options in sequence. * @return Item * @throws Exception exception */ public Item aggregate(final Str handler, final Item col, final Item first, final Value additionalOps) throws Exception { final DB db = getDbHandler(handler); AggregationOutput agg; DBObject[] pipeline = null; if (additionalOps != null && (!additionalOps.isEmpty())) { if (additionalOps instanceof Map) { pipeline = mapToDBObjectArray((Map) additionalOps); } else { int length = (int) additionalOps.size(); if (length > 0) { pipeline = new BasicDBObject[length]; int i = 0; for (Item x : additionalOps) { pipeline[i++] = getDbObjectFromItem(x); } } } } db.requestStart(); try { if (additionalOps != null && (!additionalOps.isEmpty())) { agg = db.getCollection(itemToString(col)).aggregate(getDbObjectFromItem(first), pipeline); } else { agg = db.getCollection(itemToString(col)).aggregate(getDbObjectFromItem(first)); } final Iterable<DBObject> d = agg.results(); return returnResult(handler, Str.get(JSON.serialize(d))); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(e.getMessage()); } finally { db.requestDone(); } }