List of usage examples for com.mongodb DBCollection setDBEncoderFactory
public synchronized void setDBEncoderFactory(@Nullable final DBEncoderFactory factory)
From source file:com.github.nlloyd.hornofmongo.adaptor.Mongo.java
License:Open Source License
@JSFunction public Object find(final String ns, final Object query, final Object fields, Integer limit, Integer skip, Integer batchSize, Integer options) { Object result = null;//from w ww .ja v a 2s . com Object rawQuery = BSONizer.convertJStoBSON(query, false); Object rawFields = BSONizer.convertJStoBSON(fields, false); DBObject bsonQuery = null; DBObject bsonFields = null; if (rawQuery instanceof DBObject) bsonQuery = (DBObject) rawQuery; if (rawFields instanceof DBObject) bsonFields = (DBObject) rawFields; com.mongodb.DB db = innerMongo.getDB(ns.substring(0, ns.indexOf('.'))); String collectionName = ns.substring(ns.indexOf('.') + 1); if ("$cmd".equals(collectionName)) { try { if (options == 0) options = innerMongo.getOptions(); //GC: 16/11/15 fixed for v3 // CommandResult cmdResult = db.command(bsonQuery, options, CommandResult cmdResult = db.command(bsonQuery, innerMongo.getReadPreference(), HornOfMongoBSONEncoder.FACTORY.create()); //GC: 16/11/15 removed for v3 // handlePostCommandActions(db, bsonQuery); Object jsCmdResult = BSONizer.convertBSONtoJS(mongoScope, cmdResult); result = MongoRuntime .call(new NewInstanceAction(mongoScope, "InternalCursor", new Object[] { jsCmdResult })); } catch (NoSuchElementException nse) { // thrown when db.runCommand() called (no arguments) CommandResult failedCmdResult = db.command(this.hosts.iterator().next().toString()); failedCmdResult.put("ok", Boolean.FALSE); failedCmdResult.put("errmsg", "no such cmd: "); Object jsFailedCmdResult = BSONizer.convertBSONtoJS(mongoScope, failedCmdResult); result = MongoRuntime.call( new NewInstanceAction(mongoScope, "InternalCursor", new Object[] { jsFailedCmdResult })); } catch (MongoException me) { handleMongoException(me); } } else { DBCollection collection = db.getCollection(collectionName); collection.setDBEncoderFactory(HornOfMongoBSONEncoder.FACTORY); collection.setDBDecoderFactory(HornOfMongoBSONDecoder.FACTORY); DBObject specialFields = null; if (bsonQuery.get("query") instanceof DBObject) { specialFields = bsonQuery; bsonQuery = (DBObject) bsonQuery.get("query"); } DBCursor cursor = collection.find(bsonQuery, bsonFields).skip(skip).batchSize(batchSize).limit(limit) .addOption(options); if (specialFields != null) { for (String key : specialFields.keySet()) { if (!"query".equals(key)) cursor.addSpecial(key, specialFields.get(key)); } } InternalCursor jsCursor = (InternalCursor) MongoRuntime .call(new NewInstanceAction(mongoScope, "InternalCursor", new Object[] { cursor })); result = jsCursor; } return result; }
From source file:com.github.nlloyd.hornofmongo.adaptor.Mongo.java
License:Open Source License
@SuppressWarnings({ "unchecked", "rawtypes" }) @JSFunction/* w w w . jav a 2 s. com*/ public void insert(final String ns, Object obj, int options) { Object rawObj = BSONizer.convertJStoBSON(obj, true); DBObject bsonObj = null; if (rawObj instanceof DBObject) bsonObj = (DBObject) rawObj; try { int dbSeparatorIdx = ns.indexOf('.'); com.mongodb.DB db = innerMongo.getDB(ns.substring(0, dbSeparatorIdx)); String collectionName = ns.substring(dbSeparatorIdx + 1); DBCollection collection = db.getCollection(collectionName); collection.setDBEncoderFactory(HornOfMongoBSONEncoder.FACTORY); collection.setDBDecoderFactory(HornOfMongoBSONDecoder.FACTORY); // unfortunately the Java driver does not expose the _allow_dot // argument in insert calls so we need to translate system.indexes // inserts into index creation calls through the java driver if (collectionName.endsWith("system.indexes")) { db.getCollection("system.indexes").insert(Arrays.asList(bsonObj)); } else { int oldOptions = collection.getOptions(); collection.setOptions(options); List insertObj = null; if (rawObj instanceof List) insertObj = (List) rawObj; else insertObj = Arrays.asList(rawObj); collection.insert(insertObj); collection.setOptions(oldOptions); } saveLastCalledDB(db); } catch (MongoException me) { handleMongoException(me); } }
From source file:com.github.nlloyd.hornofmongo.adaptor.Mongo.java
License:Open Source License
@JSFunction public void remove(final String ns, Object pattern, boolean justOne) { Object rawPattern = BSONizer.convertJStoBSON(pattern, false); DBObject bsonPattern = null;/*w w w.j a v a 2s . c o m*/ if (rawPattern instanceof DBObject) bsonPattern = (DBObject) rawPattern; com.mongodb.DB db = innerMongo.getDB(ns.substring(0, ns.indexOf('.'))); DBCollection collection = db.getCollection(ns.substring(ns.indexOf('.') + 1)); collection.setDBEncoderFactory(HornOfMongoBSONEncoder.FACTORY); try { collection.remove(bsonPattern); saveLastCalledDB(db); } catch (MongoException me) { handleMongoException(me); } }
From source file:com.github.nlloyd.hornofmongo.adaptor.Mongo.java
License:Open Source License
@JSFunction public void update(final String ns, Object query, Object obj, final Boolean upsert, final Boolean multi) { Object rawQuery = BSONizer.convertJStoBSON(query, false); Object rawObj = BSONizer.convertJStoBSON(obj, true); DBObject bsonQuery = null;//from w w w. j a va2 s. com DBObject bsonObj = null; if (rawQuery instanceof DBObject) bsonQuery = (DBObject) rawQuery; if (rawObj instanceof DBObject) bsonObj = (DBObject) rawObj; boolean upsertOp = (upsert != null) ? upsert : false; boolean multiOp = (multi != null) ? multi : false; com.mongodb.DB db = innerMongo.getDB(ns.substring(0, ns.indexOf('.'))); DBCollection collection = db.getCollection(ns.substring(ns.indexOf('.') + 1)); collection.setDBEncoderFactory(HornOfMongoBSONEncoder.FACTORY); try { collection.update(bsonQuery, bsonObj, upsertOp, multiOp); saveLastCalledDB(db); } catch (MongoException me) { handleMongoException(me); } }
From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoStorage.java
License:Open Source License
protected <T extends Entity> DBCollection getDBCollection(Class<T> type) { DBCollection collection = collectionCache.get(type); if (collection == null) { Class<? extends Entity> baseType = getBaseClass(type); String collectionName = getInternalName(baseType); collection = mongoDB.getCollection(collectionName); collection.setDBDecoderFactory(treeDecoderFactory); collection.setDBEncoderFactory(treeEncoderFactory); collectionCache.put(type, collection); }/*from ww w . j a v a 2s . c o m*/ return collection; }
From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoStorage.java
License:Open Source License
protected <T extends Entity> DBCollection getVersionCollection(Class<T> type) { Class<? extends Entity> baseType = getBaseClass(type); String collectionName = getInternalName(baseType) + "_versions"; DBCollection collection = mongoDB.getCollection(collectionName); collection.setDBDecoderFactory(treeDecoderFactory); collection.setDBEncoderFactory(treeEncoderFactory); return collection; }
From source file:org.jongo.Jongo.java
License:Apache License
public MongoCollection getCollection(String name) { DBCollection dbCollection = database.getCollection(name); dbCollection.setDBDecoderFactory(BsonDBDecoder.FACTORY); dbCollection.setDBEncoderFactory(BsonDBEncoder.FACTORY); return new MongoCollection(dbCollection, mapper); }
From source file:org.mongojack.JacksonDBCollection.java
License:Apache License
protected JacksonDBCollection(DBCollection dbCollection, JavaType type, JavaType keyType, ObjectMapper objectMapper, Class<?> view, Map<Feature, Boolean> features) { this.dbCollection = dbCollection; this.type = type; this.keyType = keyType; this.objectMapper = objectMapper; this.view = view; this.decoderFactory = new JacksonDecoderFactory<T>(this, objectMapper, type); // We want to find how we should serialize the ID, in case it is passed to us try {/*from w w w . j ava 2 s . c om*/ this.idHandler = (IdHandler) IdHandlerFactory.getIdHandlerForProperty(objectMapper, type); } catch (JsonMappingException e) { throw new MongoJsonMappingException("Unable to introspect class", e); } if (features == null) { this.features = new ConcurrentHashMap<Feature, Boolean>(); } else { this.features = features; } dbCollection.setDBEncoderFactory(new JacksonEncoderFactory(objectMapper, this)); }