Example usage for com.mongodb DBCollection save

List of usage examples for com.mongodb DBCollection save

Introduction

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

Prototype

public WriteResult save(final DBObject document, final WriteConcern writeConcern) 

Source Link

Document

Update an existing document or insert a document depending on the parameter.

Usage

From source file:org.qi4j.entitystore.mongodb.MongoMapEntityStoreMixin.java

License:Apache License

@Override
public void applyChanges(MapChanges changes) throws IOException {
    db.requestStart();//w  w w.ja  v a 2  s  . c  o m
    final DBCollection entities = db.getCollection(collectionName);

    changes.visitMap(new MapChanger() {

        @Override
        public Writer newEntity(final EntityReference ref, EntityDescriptor entityDescriptor)
                throws IOException {
            return new StringWriter(1000) {

                @Override
                public void close() throws IOException {
                    super.close();

                    String jsonState = toString();
                    System.out.println("############################################");
                    try {
                        System.out.println(new JSONObject(jsonState).toString(2));
                    } catch (JSONException ex) {
                        ex.printStackTrace();
                    }
                    System.out.println("############################################");
                    DBObject bsonState = (DBObject) JSON.parse(jsonState);

                    BasicDBObject entity = new BasicDBObject();
                    entity.put(IDENTITY_COLUMN, ref.identity());
                    entity.put(STATE_COLUMN, bsonState);
                    entities.save(entity, writeConcern);
                }

            };
        }

        @Override
        public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor)
                throws IOException {
            return new StringWriter(1000) {

                @Override
                public void close() throws IOException {
                    super.close();

                    DBObject bsonState = (DBObject) JSON.parse(toString());

                    BasicDBObject entity = new BasicDBObject();
                    entity.put(IDENTITY_COLUMN, ref.identity());
                    entity.put(STATE_COLUMN, bsonState);
                    entities.update(byIdentity(ref), entity, true, false, writeConcern);
                }

            };
        }

        @Override
        public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor)
                throws EntityNotFoundException {
            DBObject entity = entities.findOne(byIdentity(ref));
            if (entity == null) {
                throw new EntityNotFoundException(ref);
            }
            entities.remove(entity, writeConcern);
        }

    });

    db.requestDone();
}

From source file:simple.crawler.mongo.CrawlingDB.java

License:Open Source License

public boolean save(CrawlingDBObject obj, Collection collection) {
    DB db = client.getDB(dbName);/* w  w w  .  j a va  2s  . c o  m*/
    DBCollection con = db.getCollection(collection.toString());
    WriteResult result = con.save(obj, WriteConcern.JOURNALED);
    if (result.getError() == null) {
        return true;
    } else {
        LOG.error(result.getError());
        return false;
    }
}