Example usage for com.mongodb WriteResult toString

List of usage examples for com.mongodb WriteResult toString

Introduction

In this page you can find the example usage for com.mongodb WriteResult toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:org.basex.modules.MongoDB.java

License:BSD License

/**
 * Mongodb Save function./*from   ww  w  .j  a v a  2s .co  m*/
 * @param handler DB handler
 * @param col collection name
 * @param saveStr string to save(Map or Josn)
 * @return Item
 * @throws Exception
 */
public Item save(final Str handler, final Str col, final Item saveStr) throws Exception {
    final DB db = getDbHandler(handler);
    db.requestStart();
    try {
        WriteResult wr = db.getCollection(col.toJava()).save(getDbObjectFromStr(saveStr));
        return returnResult(handler, Str.get(wr.toString()));
    } catch (MongoException e) {
        throw MongoDBErrors.generalExceptionError(db.getLastError().getString("err"));
    } finally {
        db.requestDone();
    }
}

From source file:org.basex.modules.nosql.MongoDB.java

License:BSD License

/**
 * Insert data in MongoDB.//from  w w  w . j  a  v  a 2s .c  o  m
 * @param handler database handler DB Handler
 * @param col collection name
 * @param insertString string to insert in json formart or in Basex's Map
 * @return Item result from Mongodb.
 * @throws Exception exception
 */
public Item insert(final Str handler, final Str col, final Str insertString) throws Exception {
    final DB db = getDbHandler(handler);
    db.requestStart();
    try {
        DBObject obj = getDbObjectFromItem(insertString);
        WriteResult wr = db.getCollection(col.toJava()).insert(obj);
        return returnResult(handler, Str.get(wr.toString()));
    } catch (MongoException e) {
        throw MongoDBErrors.generalExceptionError(db.getLastError().getString("err"));
    } finally {
        db.requestDone();
    }
}

From source file:org.basex.modules.nosql.MongoDB.java

License:BSD License

/**
 * Mongodb update with 4 parameters like update({},{}, upsert, multi).
 * @param handler database handler Db Handler string
 * @param col collection name//  w  w  w. ja  v  a 2 s.  co m
 * @param query selection query
 * @param updatestring String to be updated
 * @param upsert true/false for mongodb upsert(Json Str or Map)
 * @param multi true/false for mongodb multi
 * @return Item
 * @throws Exception exception
 */
public Item update(final Str handler, final Item col, final Item query, final Item updatestring,
        final Bln upsert, final Bln multi) throws Exception {
    final DB db = getDbHandler(handler);
    db.requestStart();
    try {
        DBObject q = getDbObjectFromItem(query);
        DBObject updateValue = getDbObjectFromItem(updatestring);
        WriteResult wr;
        if (upsert != null && multi != null) {
            wr = db.getCollection(itemToString(col)).update(q, updateValue, upsert.toJava(), multi.toJava());
        } else {
            wr = db.getCollection(itemToString(col)).update(q, updateValue);
        }
        return returnResult(handler, Str.get(wr.toString()));
    } catch (MongoException e) {
        throw MongoDBErrors.generalExceptionError(db.getLastError().getString("err"));
    } finally {
        db.requestDone();
    }
}

From source file:org.basex.modules.nosql.MongoDB.java

License:BSD License

/**
 * Mongodb Save function./*from   w  w w.j a  va 2  s  .  co m*/
 * @param handler database handler DB handler
 * @param col collection name
 * @param saveStr string to save(Map or Josn)
 * @param options other writeconcern options
 * @return Item result as item
 * @throws Exception exception
 */
public Item save(final Str handler, final Str col, final Item saveStr, final Map options) throws Exception {
    final DB db = getDbHandler(handler);
    db.requestStart();
    try {
        if (options == null) {
            WriteResult wr = db.getCollection(col.toJava()).save(getDbObjectFromItem(saveStr));
            return returnResult(handler, Str.get(wr.toString()));
        }
        //TODO write concern from the options
        WriteResult wr = db.getCollection(col.toJava()).save(getDbObjectFromItem(saveStr), WriteConcern.SAFE);
        return returnResult(handler, Str.get(wr.toString()));

    } catch (MongoException e) {
        throw MongoDBErrors.generalExceptionError(db.getLastError().getString("err"));
    } finally {
        db.requestDone();
    }
}

From source file:org.dizitart.no2.datagate.services.UserAccountService.java

License:Apache License

public void delete(String username) {
    WriteResult remove = this.userRepository.remove("{userName: '" + username + "'}");
    log.info("Removed " + remove.toString());
}

From source file:org.elasticsearch.river.mongodb.simple.RiverMongoCollectionWithDot.java

License:Apache License

@Test
public void collectionWithDot_Issue206() throws Throwable {
    logger.debug("Start collectionWithDot_Issue206");
    long timestamp = System.currentTimeMillis();
    String database = "db-" + timestamp;
    try {/* ww  w. ja  va  2  s .  c  o  m*/
        String collection = "collection." + timestamp;
        createDatabase(database, collection);
        createRiver(database, collection);
        DBObject dbObject = new BasicDBObject("name", "richard-" + timestamp);
        WriteResult result = mongoCollection.insert(dbObject);
        Thread.sleep(wait);
        String id = dbObject.get("_id").toString();
        logger.info("WriteResult: {}", result.toString());
        logger.info("dbObject: {}", dbObject.toString());
        ActionFuture<IndicesExistsResponse> response = getNode().client().admin().indices()
                .exists(new IndicesExistsRequest(collection));
        assertThat(response.actionGet().isExists(), equalTo(true));
        refreshIndex(collection);
        logger.info("Request: [{}] - count: [{}]", getRequest(collection).id(id),
                getNode().client().count(countRequest(collection)).get().getCount());
        GetResponse getResponse = getNode().client().get(getRequest(collection).id(id)).get();
        assertThat(getResponse.isExists(), equalTo(true));

        mongoCollection.remove(dbObject, WriteConcern.REPLICAS_SAFE);

        Thread.sleep(wait);
        refreshIndex(collection);
        getResponse = getNode().client().get(getRequest(collection).id(id)).get();
        assertThat(getResponse.isExists(), equalTo(false));

    } catch (Throwable t) {
        logger.error("collectionWithDot_Issue206 failed.", t);
        t.printStackTrace();
        throw t;
    } finally {
        cleanUp();
    }
}

From source file:org.exist.mongodb.xquery.mongodb.collection.Insert.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {//  ww w. j a  v a 2s .  c o m
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        // Get database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // Place holder for all results
        List<DBObject> allContent = new ArrayList<>();

        SequenceIterator iterate = args[3].iterate();
        while (iterate.hasNext()) {
            String value = iterate.nextItem().getStringValue();
            if (StringUtils.isEmpty(value)) {
                LOG.error("Skipping empty string");
            } else {
                BasicDBObject bsonContent = (BasicDBObject) JSON.parse(value);
                allContent.add(bsonContent);
            }
        }

        WriteResult result = dbcol.insert(allContent);

        return new StringValue(result.toString());

    } catch (MongoCommandException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0005, ex.getMessage());

    } catch (JSONParseException ex) {
        String msg = "Invalid JSON data: " + ex.getMessage();
        LOG.error(msg);
        throw new XPathException(this, MongodbModule.MONG0004, msg);

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
        throw new XPathException(this, MongodbModule.MONG0003, t.getMessage());
    }

}

From source file:org.exist.mongodb.xquery.mongodb.collection.Remove.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/*from ww w. j a  va2 s. com*/
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        BasicDBObject query = (args.length >= 4)
                ? (BasicDBObject) JSON.parse(args[3].itemAt(0).getStringValue())
                : null;

        // Get collection in database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // Execute query      
        WriteResult result = dbcol.remove(query);

        return new StringValue(result.toString());

    } catch (JSONParseException ex) {
        LOG.error(ex.getMessage());
        throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage());

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage());
    }

}

From source file:org.exist.mongodb.xquery.mongodb.collection.Save.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/*from  ww  w.j a v a2 s . c o m*/
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        // Get database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // Get data
        BasicDBObject data = (BasicDBObject) JSON.parse(args[3].itemAt(0).getStringValue());

        // Execute save
        WriteResult result = dbcol.save(data);

        return new StringValue(result.toString());

    } catch (MongoCommandException ex) {
        // TODO return as value?
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0005, ex.getMessage());

    } catch (JSONParseException ex) {
        String msg = "Invalid JSON data: " + ex.getMessage();
        LOG.error(msg);
        throw new XPathException(this, MongodbModule.MONG0004, msg);

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
        throw new XPathException(this, MongodbModule.MONG0003, t.getMessage());
    }

}

From source file:org.exist.mongodb.xquery.mongodb.collection.Update.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {//from  www. ja va 2 s .  c om
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        // Get database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // Get data
        BasicDBObject criterium = (BasicDBObject) JSON.parse(args[3].itemAt(0).getStringValue());
        BasicDBObject modification = (BasicDBObject) JSON.parse(args[4].itemAt(0).getStringValue());

        Boolean upsert = (args.length >= 6) ? args[5].itemAt(0).toJavaObject(Boolean.class) : null;

        Boolean multi = (args.length >= 7) ? args[6].itemAt(0).toJavaObject(Boolean.class) : null;

        // Execute update
        WriteResult update = (upsert == null) ? dbcol.update(criterium, modification)
                : dbcol.update(criterium, modification, upsert, multi);

        return new StringValue(update.toString());

    } catch (MongoCommandException ex) {
        // TODO return as value?
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0005, ex.getMessage());

    } catch (JSONParseException ex) {
        String msg = "Invalid JSON data: " + ex.getMessage();
        LOG.error(msg);
        throw new XPathException(this, MongodbModule.MONG0004, msg);

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
        throw new XPathException(this, MongodbModule.MONG0003, t.getMessage());
    }

}