List of usage examples for com.mongodb.util JSON parse
public static Object parse(final String jsonString)
Parses a JSON string and returns a corresponding Java object.
From source file:mongosensors_server.MapReduce.java
License:Open Source License
public String[] getBUSInfo(String title, String[] time) { //POSITIONS ID:LOC String map = "function () {" + "value = {loc : [this.loc]} ;" + "emit(" + this.id + ", value);" + "}"; String reduce = "function(key, values) {" + "title_list = { loc: [] };" + "for(var i in values) {" + "title_list.loc = values[i].loc.concat(title_list.loc);" + "}" + "return title_list;" + "}"; DBObject query = (DBObject) JSON .parse("{'Last update':{$gt:'" + time[0] + "',$lt:'" + time[1] + "'},'title':'" + title + "'}"); List<String> stringValue = new ArrayList<String>(); int i = 0;// www. j ava 2 s . c o m for (DBObject o : ((MongoTools) this.client).mongoMapReduce(this.collection, map, reduce, query) .results()) { stringValue.add(o.toString()); i++; } String[] array = new String[stringValue.size()]; stringValue.toArray(array); return array; }
From source file:mongosensors_server.MongoTools.java
License:Open Source License
public void insertInCollection(String collection, JSONObject json) { DBObject jsonDoc = (DBObject) JSON.parse(json.toString()); this.db.getCollection(collection).insert(jsonDoc); }
From source file:mongosensors_server.MongoTools.java
License:Open Source License
public void insertInCollection(String collection, String data) { DBObject jsonDoc = (DBObject) JSON.parse(data); this.db.getCollection(collection).insert(jsonDoc); }
From source file:mongosensors_server.MongoTools.java
License:Open Source License
public DBCursor getFromCollection(String collection, String query) { return this.db.getCollection(collection).find((DBObject) JSON.parse(query)); }
From source file:mongosensors_server.MongoTools.java
License:Open Source License
public void removeFromCollection(String collection, String query) { this.db.getCollection(collection).remove((DBObject) JSON.parse(query)); }
From source file:mx.edu.cide.justiciacotidiana.v1.model.Comentario.java
License:Open Source License
/** * Genera un BasicDBObject a partir de una cadena JSON que representa un comentario. * @param content Cadena con el JSON del comentario. * @return BasicDBObject si el comentario es vlido y contiene al menos los campos requeridos, null en otro caso. */// www . j ava2 s .c om public static BasicDBObject parse(String content) { BasicDBObject obj = (BasicDBObject) JSON.parse(content); BasicDBObject from = (BasicDBObject) obj.get(FIELDS.FROM); String checkedVal = ""; List<String> params = new ArrayList<String>(); params.add(FIELDS.MESSAGE); params.add(FIELDS.PROPOSALID); if (!Utils.validateEmptyStringFields(obj, params)) { return null; } if (null == from) { return null; } checkedVal = from.getString(FIELDS.FACEBOOKUSER); if (null == checkedVal || checkedVal.length() == 0) return null; //Remove unmodifiable fields checkedVal = obj.getString(FIELDS.CREATED); if (null != checkedVal) obj.remove(FIELDS.CREATED); checkedVal = obj.getString(FIELDS.UPDATED); if (null != checkedVal) obj.remove(FIELDS.UPDATED); checkedVal = obj.getString(FIELDS.SWBPAGE); if (null != checkedVal) obj.remove(FIELDS.SWBPAGE); return obj; }
From source file:mx.edu.cide.justiciacotidiana.v1.model.Voto.java
License:Open Source License
/** * Genera un BasicDBObject a partir de una cadena JSON que representa un voto. * @param content Cadena con el JSON del voto. * @return BasicDBObject si el voto es vlido y contiene al menos los campos requeridos, null en otro caso. *///ww w . ja v a 2 s .c o m public static BasicDBObject parse(String content) { BasicDBObject obj = (BasicDBObject) JSON.parse(content); List<String> params = new ArrayList<String>(); params.add(FIELDS.PROPOSALID); params.add(FIELDS.VALUE); params.add(FIELDS.FACEBOOKUSER); if (!Utils.validateEmptyStringFields(obj, params)) { return null; } String val = obj.getString(FIELDS.VALUE); if (!LIKE.equalsIgnoreCase(val) && !DISLIKE.equalsIgnoreCase(val) && !REFRAIN.equalsIgnoreCase(val)) return null; //Remove unmodifiable fields if (null == obj.getString(FIELDS.CREATED)) obj.remove(FIELDS.CREATED); return obj; }
From source file:net.hydromatic.optiq.impl.mongodb.MongoTable.java
License:Apache License
/** Executes a "find" operation on the underlying collection. * * <p>For example,//from w w w . j ava 2s . com * <code>zipsTable.find("{state: 'OR'}", "{city: 1, zipcode: 1}")</code></p> * * @param mongoDb MongoDB connection * @param filterJson Filter JSON string, or null * @param projectJson Project JSON string, or null * @param fields List of fields to project; or null to return map * @return Enumerator of results */ public Enumerable<Object> find(DB mongoDb, String filterJson, String projectJson, final List<String> fields) { final DBCollection collection = mongoDb.getCollection(collectionName); final DBObject filter = filterJson == null ? null : (DBObject) JSON.parse(filterJson); final DBObject project = projectJson == null ? null : (DBObject) JSON.parse(projectJson); final Function1<DBObject, Object> getter = MongoEnumerator.getter(fields); return new AbstractEnumerable<Object>() { public Enumerator<Object> enumerator() { final DBCursor cursor = collection.find(filter, project); return new MongoEnumerator(cursor, getter); } }; }
From source file:net.hydromatic.optiq.impl.mongodb.MongoTable.java
License:Apache License
/** Executes an "aggregate" operation on the underlying collection. * * <p>For example://from w w w . j a v a 2 s.c o m * <code>zipsTable.aggregate( * "{$filter: {state: 'OR'}", * "{$group: {_id: '$city', c: {$sum: 1}, p: {$sum: '$pop'}}}") * </code></p> * * @param mongoDb MongoDB connection * @param fields List of fields to project; or null to return map * @param operations One or more JSON strings * @return Enumerator of results */ public Enumerable<Object> aggregate(final DB mongoDb, final List<String> fields, final List<String> operations) { List<DBObject> list = new ArrayList<DBObject>(); for (String operation : operations) { list.add((DBObject) JSON.parse(operation)); } final DBObject first = list.get(0); final List<DBObject> rest = Util.skip(list); final Function1<DBObject, Object> getter = MongoEnumerator.getter(fields); return new AbstractEnumerable<Object>() { public Enumerator<Object> enumerator() { final AggregationOutput result; try { result = mongoDb.getCollection(collectionName).aggregate(first, rest.toArray(new DBObject[rest.size()])); } catch (Exception e) { throw new RuntimeException( "While running MongoDB query " + Util.toString(operations, "[", ",\n", "]"), e); } return new MongoEnumerator(result.results().iterator(), getter); } }; }
From source file:net.jurre.edutil.persistence.MongoDB.java
License:Open Source License
public void saveEDDNData(String data) { BasicDBObject dataObj = (BasicDBObject) JSON.parse(data); String schema = dataObj.getString("$schemaRef"); dataObj.removeField("$schemaRef"); dataObj.append("schemaRef", schema); if (dataObj.getString("schemaRef").equalsIgnoreCase("http://schemas.elite-markets.net/eddn/commodity/2")) { BasicDBObject message = (BasicDBObject) dataObj.get("message"); boolean stationupdated = updateStationMarketData(message.getString("stationName"), message.getString("systemName"), message.get("commodities")); if (stationupdated) dataObj.append("stationUpdated", true); }//ww w . j a v a 2 s .co m DBCollection collection = this.db.getCollection(MongoDB.EDDN_COLLECTION); collection.insert(dataObj); }