Example usage for com.mongodb BasicDBObjectBuilder start

List of usage examples for com.mongodb BasicDBObjectBuilder start

Introduction

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

Prototype

public static BasicDBObjectBuilder start() 

Source Link

Document

Creates a builder intialized with an empty document.

Usage

From source file:dao.MongoDBSquareDao.java

public Square readSquare(String id) {
    DBObject query = BasicDBObjectBuilder.start().append("_id", new ObjectId(id)).get();
    DBObject data = this.col.findOne(query);
    return SquareConverter.toSquare(data);
}

From source file:de.belaso.mongolyn.ui.TaskDataHandler.java

License:Open Source License

@Override
public RepositoryResponse postTaskData(TaskRepository repository, TaskData taskData,
        Set<TaskAttribute> oldAttributes, IProgressMonitor monitor) throws CoreException {
    DBCollection dbCollection = MongolynUtils.getDBCollection(repository);
    BasicDBObjectBuilder bob = BasicDBObjectBuilder.start();
    for (Map.Entry<String, TaskAttribute> entry : taskData.getRoot().getAttributes().entrySet()) {
        String key = entry.getKey();
        TaskAttribute attribute = entry.getValue();
        String attributeValue = attribute.getValue();
        if (attributeValue != null) {
            bob.add(key.replace('.', '_'), attributeValue);
        }//from   w w w  . ja v  a  2s .c o m
    }
    DBObject dbObject = bob.get();
    try {
        if (taskData.isNew()) {
            dbCollection.insert(dbObject, WriteConcern.SAFE);
            return new RepositoryResponse(ResponseKind.TASK_CREATED, dbObject.get("_id").toString());
        } else {
            dbCollection.findAndModify(new BasicDBObject("_id", new ObjectId(taskData.getTaskId())), dbObject);
            return new RepositoryResponse(ResponseKind.TASK_UPDATED, taskData.getTaskId());
        }
    } catch (MongoException mongoException) {
        throw new CoreException(Activator.INSTANCE.getErrorStatus(mongoException));
    }
}

From source file:de.flapdoodle.mongoom.datastore.Indexes.java

License:Apache License

public static void ensureIndex(DB db, IndexDef index, String collectionName) {

    // public <T> void ensureIndex(Class<T> clazz, String name,
    // Set<IndexFieldDef> defs, boolean unique,
    // boolean dropDupsOnCreate) {
    BasicDBObjectBuilder keys = BasicDBObjectBuilder.start();
    BasicDBObjectBuilder keyOpts = null;
    List<FieldIndex> indexSorted = Lists.newArrayList(index.fields());
    Collections.sort(indexSorted, new Comparator<FieldIndex>() {
        @Override/*from  ww w .j  a v a2  s.c  o m*/
        public int compare(FieldIndex o1, FieldIndex o2) {
            if (o1.priority() == o2.priority())
                return 0;
            if (o1.priority() < o2.priority())
                return 1;
            return -1;
        }
    });

    for (FieldIndex def : indexSorted) {
        String fieldName = def.name();
        Direction dir = def.direction();
        if (dir == Direction.BOTH)
            keys.add(fieldName, 1).add(fieldName, -1);
        else
            keys.add(fieldName, (dir == Direction.ASC) ? 1 : -1);
    }

    String name = index.name();

    if (name != null && !name.isEmpty()) {
        if (keyOpts == null)
            keyOpts = new BasicDBObjectBuilder();
        keyOpts.add("name", name);
    }
    if (index.unique()) {
        if (keyOpts == null)
            keyOpts = new BasicDBObjectBuilder();
        keyOpts.add("unique", true);
        if (index.dropDups())
            keyOpts.add("dropDups", true);
    }
    if (index.sparse()) {
        if (keyOpts == null)
            keyOpts = new BasicDBObjectBuilder();
        keyOpts.add("sparse", true);
    }

    try {
        db.requestStart();
        DBCollection dbColl = db.getCollection(collectionName);
        DBObject indexKeys = keys.get();
        //         DatastoreImpl._logger.info("Ensuring index for " + dbColl.getName() + "." + index + " with keys " + indexKeys);
        if (keyOpts == null) {
            _logger.info("Ensuring index for " + dbColl.getName() + "." + index + " with keys " + indexKeys);
            dbColl.ensureIndex(indexKeys);
        } else {
            DBObject options = keyOpts.get();
            _logger.info("Ensuring index for " + dbColl.getName() + "." + index + " with keys " + indexKeys
                    + " and opts " + options);
            dbColl.ensureIndex(indexKeys, options);
        }
    } finally {
        Errors.checkError(db, Operation.Insert);
        db.requestDone();
    }
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static void CreateCol(String tempColName, MongoClient mongoClient) {
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBObject options = BasicDBObjectBuilder.start().add("capped", true).add("size", 2000000000l).get();
    db.createCollection(tempColName, options);
    //mongoClient.close();
}

From source file:es.bsc.amon.controller.EventsDBMapper.java

License:Open Source License

public String getString(String id) {
    BasicDBObjectBuilder b = BasicDBObjectBuilder.start();
    b.add(_ID, new ObjectId(id));

    DBCursor cur = colEvents.find(b.get());
    if (cur.hasNext()) {
        return cur.next().toString();
    }//from  w  ww.  ja  v a2  s.c o  m
    return null;
}

From source file:es.bsc.amon.controller.EventsDBMapper.java

License:Open Source License

public void delete(String id) {
    BasicDBObjectBuilder q = BasicDBObjectBuilder.start();
    q.add(_ID, new ObjectId(id));
    colEvents.remove(q.get());
}

From source file:es.bsc.amon.controller.EventsDBMapper.java

License:Open Source License

public ObjectNode markAsFinished(String id) {
    BasicDBObjectBuilder q = BasicDBObjectBuilder.start();
    q.add(_ID, new ObjectId(id));
    long timestamp = Calendar.getInstance().getTimeInMillis();

    BasicDBObjectBuilder m = BasicDBObjectBuilder.start();
    m.add("$set", BasicDBObjectBuilder.start(ENDTIME, timestamp).get());

    colEvents.update(q.get(), m.get(), false, false);

    ObjectNode on = new ObjectNode(JsonNodeFactory.instance);
    on.put(_ID, id);/* w  w  w. j  av a 2s  . c o m*/
    on.put(ENDTIME, timestamp);

    return on;
}

From source file:es.bsc.amon.controller.EventsDBMapper.java

License:Open Source License

public void remove(String id) {
    BasicDBObjectBuilder q = BasicDBObjectBuilder.start().add(_ID, new ObjectId(id));
    colEvents.findAndRemove(q.get());
}

From source file:essex.bigessexnew.TwitterStreamHandler.java

@Override
public void run() {
    StatusListener sl = new StatusListener() {
        @Override/*from w  ww .ja  v  a 2 s .c  om*/
        public void onStatus(Status status) {
            /*String s = TwitterObjectFactory.getRawJSON(status);
            System.out.println(s);*/
            BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start();
            documentBuilder.add("content", status.getText());
            collection.insert(documentBuilder.get());

            /*DBObject dbObject = (DBObject) JSON.parse(json);
            collection.insert(dbObject);*/
            //System.out.println(json.toString());

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {

        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {

        }

        @Override
        public void onScrubGeo(long l, long l2) {

        }

        @Override
        public void onStallWarning(StallWarning stallWarning) {

        }

        @Override
        public void onException(Exception ex) {

        }
    };
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(sl);
    twitterStream.filter(Params.getProperty("hashtag"));
}

From source file:ezbake.services.centralPurge.helpers.EzCentralPurgeServiceHelpers.java

License:Apache License

public static DBObject encodeDateTime(DateTime dateTime) {
    BasicDBObjectBuilder timeZoneBuilder = BasicDBObjectBuilder.start();

    Date date = dateTime.getDate();
    Time time = dateTime.getTime();
    TimeZone timeZone = time.getTz();

    timeZoneBuilder.add(Hour, timeZone.getHour());
    timeZoneBuilder.add(Minute, timeZone.getMinute());
    timeZoneBuilder.add(AfterUTC, timeZone.isAfterUTC());

    BasicDBObjectBuilder basicDBObjectBuilder = BasicDBObjectBuilder.start().add(Year, date.getYear())
            .add(Month, date.getMonth()).add(Day, date.getDay()).add(Hour, time.getHour())
            .add(Minute, time.getMinute()).add(Second, time.getSecond()).add(Millisecond, time.getMillisecond())
            .add(Timezone, timeZoneBuilder.get());

    return basicDBObjectBuilder.get();
}