Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

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

Prototype

@Deprecated
public Mongo() 

Source Link

Document

Creates a Mongo instance based on a (single) mongodb node (localhost, default port)

Usage

From source file:org.einherjer.week2.samples.FieldSelectionSample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    Mongo client = new Mongo();
    DB db = client.getDB("course");
    DBCollection collection = db.getCollection("fieldSelectionSample");
    collection.drop();//w  w  w. java2 s . com
    Random rand = new Random();

    // insert 10 documents with two random integers
    for (int i = 0; i < 10; i++) {
        collection.insert(new BasicDBObject("x", rand.nextInt(2)).append("y", rand.nextInt(100)).append("z",
                rand.nextInt(1000)));
    }

    DBObject query = QueryBuilder.start("x").is(0).and("y").greaterThan(10).lessThan(70).get();

    //use second find parameter to filter the returned fields
    DBCursor cursor = collection.find(query, new BasicDBObject("y", true).append("_id", false));
    try {
        while (cursor.hasNext()) {
            DBObject cur = cursor.next();
            System.out.println(cur);
        }
    } finally {
        cursor.close();
    }
}

From source file:org.einherjer.week2.samples.FindAndModifySample.java

License:Apache License

private static DBCollection createCollection() throws UnknownHostException {
    Mongo client = new Mongo();
    DB db = client.getDB("course");
    return db.getCollection("findModifySample");
}

From source file:org.einherjer.week2.samples.FindCriteriaSample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    Mongo client = new Mongo();
    DB db = client.getDB("course");
    DBCollection collection = db.getCollection("findCriteriaSample");
    collection.drop();/*  ww  w  .  j a  v a 2s.  c  om*/

    // insert 10 documents with two random integers
    for (int i = 0; i < 10; i++) {
        collection
                .insert(new BasicDBObject("x", new Random().nextInt(2)).append("y", new Random().nextInt(100)));
    }

    //1- The query document can be created by using a QueryBuilder...
    QueryBuilder builder = QueryBuilder.start("x").is(0).and("y").greaterThan(10).lessThan(70);
    //2- or, the query document can be created manually
    DBObject query = new BasicDBObject("x", 0).append("y", new BasicDBObject("$gt", 10).append("$lt", 90));

    System.out.println("\nCount:");
    long count = collection.count(builder.get());
    System.out.println(count);

    System.out.println("\nFind all: ");
    DBCursor cursor = collection.find(builder.get());
    try {
        while (cursor.hasNext()) {
            DBObject cur = cursor.next();
            System.out.println(cur);
        }
    } finally {
        cursor.close();
    }
}

From source file:org.einherjer.week2.samples.FindSample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    Mongo client = new Mongo();
    DB db = client.getDB("course");
    DBCollection collection = db.getCollection("findSample");
    collection.drop();//from   w w  w  .  j  av a  2s .c o m

    // insert 10 documents with a random integer as the value of field "x"
    for (int i = 0; i < 10; i++) {
        collection.insert(new BasicDBObject("x", new Random().nextInt(100)));
    }

    System.out.println("Find one:");
    DBObject one = collection.findOne();
    System.out.println(one);

    System.out.println("\nFind all: ");
    DBCursor cursor = collection.find();
    try {
        while (cursor.hasNext()) {
            DBObject cur = cursor.next();
            System.out.println(cur);
        }
    } finally {
        cursor.close();
    }

    System.out.println("\nCount:");
    long count = collection.count();
    System.out.println(count);
}

From source file:org.einherjer.week2.samples.InsertSample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    Mongo client = new Mongo();
    DB courseDB = client.getDB("course");
    DBCollection collection = courseDB.getCollection("insertSample");

    collection.drop();/*from w  ww .  j a  v a  2s.c o m*/

    DBObject doc = new BasicDBObject().append("x", 1);
    System.out.println(doc);
    collection.insert(doc);
    System.out.println(doc); //_id field gets generated

    DBObject doc2 = new BasicDBObject("_id", new ObjectId()).append("x", 1);
    System.out.println(doc2);
    collection.insert(doc2);
    System.out.println(doc2); //same as doc1

    DBObject doc3 = new BasicDBObject("_id", "123").append("x", 1);
    System.out.println(doc3);
    collection.insert(doc3);
    System.out.println(doc3); //_id field set by to a type different than ObjectId

    collection.insert(doc); //duplicate exception

}

From source file:org.einherjer.week2.samples.SortSkipLimitSample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    Mongo client = new Mongo();
    DB db = client.getDB("course");
    DBCollection lines = db.getCollection("sortSkipLimitSample");
    lines.drop();//from  w  ww .j a v  a2s.  c  o  m
    Random rand = new Random();

    // insert 10 lines with random start and end points
    for (int i = 0; i < 10; i++) {
        lines.insert(new BasicDBObject("_id", i)
                .append("start", new BasicDBObject("x", rand.nextInt(2)).append("y", rand.nextInt(90) + 10))
                .append("end", new BasicDBObject("x", rand.nextInt(2)).append("y", rand.nextInt(90) + 10)));
    }

    DBCursor cursor = lines.find().sort(new BasicDBObject("start.x", 1).append("start.y", -1)).skip(2)
            .limit(10);

    try {
        while (cursor.hasNext()) {
            DBObject cur = cursor.next();
            System.out.println(cur);
        }
    } finally {
        cursor.close();
    }
}

From source file:org.einherjer.week2.samples.UpdateRemoveSample.java

License:Apache License

private static DBCollection createCollection() throws UnknownHostException {
    Mongo client = new Mongo();
    DB db = client.getDB("course");
    DBCollection collection = db.getCollection("updateRemoveSample");
    collection.drop();/*from   www  .j  av  a2 s.  c  om*/
    return collection;
}

From source file:org.eobjects.analyzer.storage.MongoDbStorageProvider.java

License:Open Source License

@Override
public RowAnnotationFactory createRowAnnotationFactory() {
    try {//from ww w . j  av  a2s.  c  om
        Mongo mongo = new Mongo();
        DB db = mongo.getDB("analyzerbeans");
        String name = "rowannotationfactory" + id.getAndIncrement();
        DBCollection collection = db.createCollection(name, null);

        // drop the collection in case it already exists
        collection.drop();
        collection = db.createCollection(name, null);

        return new MongoDbRowAnnotationFactory(collection);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.gitective.mongo.MongoUtils.java

License:Open Source License

/**
 * Get DB with given name using default connection
 * //from w w  w.  ja va 2 s  .co m
 * @param name
 * @return database
 */
public static DB getDB(String name) {
    try {
        return new Mongo().getDB(name);
    } catch (UnknownHostException e) {
        throw new IllegalArgumentException(e);
    } catch (MongoException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:org.graylog2.hipchatalarmcallback.callback.GraylogMongoClient.java

License:Open Source License

/**
 * Gets the ID of the Graylog stream. /*from w w w.  j a  v  a  2 s. co  m*/
 * The stream is identified by the alarm's topic. 
 * @param alarm Object of the alarm that was triggered.
 * @return The stream id. 
 */
public String getStreamId(Alarm alarm) {

    String streamId = null;
    try {
        Mongo mongo = new Mongo();
        // the streams collection
        DBCollection col = mongo.getDB("graylog2").getCollection("streams");
        // get the name of the current stream
        String title = getStreamName(alarm);
        // find the stream by name (title = stream name)
        BasicDBObject query = new BasicDBObject("title", title);
        LOG.debug("query: " + query);
        DBObject message = col.findOne(query);
        if (message != null) {
            // get the stream id
            ObjectId id = (ObjectId) message.get("_id");
            streamId = id.toString();
        } else {
            LOG.debug("query did not return any documents.");
        }

    } catch (UnknownHostException e) {
        LOG.error("unable to query mongodb", e);
        streamId = null;
    } catch (MongoException e) {
        LOG.error("unable to query mongodb", e);
        streamId = null;
    }

    return streamId;
}