Example usage for com.mongodb DBCollection insert

List of usage examples for com.mongodb DBCollection insert

Introduction

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

Prototype

public WriteResult insert(final List<? extends DBObject> documents) 

Source Link

Document

Insert documents into a collection.

Usage

From source file:dao.component.TrabajadorDAO.java

@Override
public String insertartrabajador(TrabajadorTO trabajador) {
    String rpta = null;//  w w  w  .ja  va  2 s.co  m
    DBCollection table = db.getCollection("trabajador");
    BasicDBObject document = new BasicDBObject();
    document.put("nombre", trabajador.getNombre());
    document.put("genero", trabajador.getGenero());
    document.put("dni", trabajador.getDni());
    document.put("fecha_nac", trabajador.getFecha_nac());
    document.put("estado_civil", trabajador.getEstado_civil());
    document.put("estado", trabajador.getEstado());
    document.put("cuenta", trabajador.getCuenta());
    document.put("seguro_invalidez", trabajador.getSeguro_invalidez());
    document.put("comision", trabajador.getComision());
    document.put("idAFP", new ObjectId(trabajador.getIdAFP()));
    document.put("idCategoria", trabajador.getIdCategoria());
    document.put("idPuesto", trabajador.getIdPuesto());
    table.insert(document);
    return rpta;
}

From source file:DataAccess.DAO.LoginDAO.java

public String validate(String username, String password) {

    String returnText = LOGIN_FAILURE;

    Mongo mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("Restaurant");

    try {/* w ww  .jav a2 s  .  co m*/

        //boolean auth = db.authenticate(username, password.toCharArray());
        MongoCredential credential2 = MongoCredential.createCredential(username, "Restaurant",
                password.toCharArray());
        MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential2));

        DB db2 = mongoClient.getDB("Restaurant");
        System.out.println("Connect to database successfully");

        DBCollection coll = db2.getCollection("Users");
        System.out.println("Collection users selected successfully");
        BasicDBObject document2 = new BasicDBObject();
        document2.put("UserName", username);
        document2.put("Password", password);
        //coll.insert(document2);
        DBCursor cur2 = coll.find(document2);

        System.out.println(cur2.toString());

        while (cur2.hasNext()) {
            System.out.println(cur2.next());
        }

        System.out.println("Login is successful!");

        if (credential2 != null) {

            DBCollection table = db.getCollection("Users");

            BasicDBObject document = new BasicDBObject();
            document.put("UserName", username);
            table.insert(document);
            DBCursor cur = table.find(document);

            while (cur.hasNext()) {
                System.out.println(cur.next());
            }

            HttpSession session = SessionBean.getSession();
            session.setAttribute("UserName", user);

            System.out.println("Login is successful!");
            returnText = LOGIN_SUCCESS;

            return "admins";
        } else {

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Incorrect Username and Passowrd", "Please enter correct username and Password"));

            System.out.println("Login is failed!");
            return "login";
        }
        //System.out.println("Done");

        //return returnText;

    } catch (MongoException e) {
        e.printStackTrace();
    } finally {
        mongo.close();
    }
    return returnText;
}

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

License:Apache License

private <T> void store(Operation operation, T entity) {
    IEntityTransformation<T> converter = _transformations.transformation((Class<T>) entity.getClass());
    DBCollection dbCollection = _db.getCollection(converter.collection().name());
    Object idValue = converter.getId(entity);
    Object versionValue = converter.getVersion(entity);

    //      if (idValue == null)
    //         throw new MappingException(entity.getClass(), "Key is NULL");
    //      DBObject convertedEntity = converter.convertTo(entity);

    BasicDBObject key = new BasicDBObject();
    key.put(Const.ID_FIELDNAME, idValue);
    if (versionValue != null)
        key.put(Const.VERSION_FIELDNAME, versionValue);

    boolean reReadId = true;
    boolean mustHaveObjectId = false;
    boolean update = false;

    switch (operation) {
    case Delete://from  w  w  w. ja va 2  s  .  c o  m
        mustHaveObjectId = true;
        reReadId = false;
        break;
    case Save:
        mustHaveObjectId = true;
        break;
    case Update:
        reReadId = false;
        update = true;
        if (idValue == null)
            throw new MappingException(entity.getClass(), "Can not update Entities with Id not set");
        break;
    }

    try {
        _db.requestStart();
        if (mustHaveObjectId) {
            if ((idValue != null) && (!(idValue instanceof ObjectId))) {
                throw new MappingException(entity.getClass(), "Can not save Entities with custom Id");
            }
        }

        converter.newVersion(entity);
        DBObject convertedEntity = converter.asObject(entity);

        switch (operation) {
        case Insert:
            _logger.fine("Insert: " + convertedEntity);
            if (idValue != null) {
                _logger.log(Level.WARNING, "Insert with Id set: " + idValue, new Exception());
            }
            dbCollection.insert(convertedEntity);
            break;
        case Update:
            _logger.fine("Update: " + convertedEntity + " (Id: " + idValue + ")");
            //               BasicDBObject updateQuery=new BasicDBObject();
            //               updateQuery.put(Const.ID_FIELDNAME, idValue);
            dbCollection.update(key, convertedEntity, false, false);
            break;
        case Save:
            _logger.fine("Save: " + convertedEntity);
            dbCollection.save(convertedEntity);
            break;
        case Delete:
            _logger.fine("Delete: " + key);
            dbCollection.remove(key);
            break;
        default:
            throw new ObjectMapperException("Operation not supported: " + operation);
        }

        if (reReadId) {
            Object savedIdValue = convertedEntity.get(Const.ID_FIELDNAME);
            converter.setId(entity, savedIdValue);
        }

        Errors.checkError(_db, operation);

        if (operation == Operation.Delete) {
            converter.setId(entity, null);
        }
    } finally {
        _db.requestDone();
    }

}

From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java

License:Open Source License

public static void Packagedocumentinsert(String id, String FQN, String name, String container) {

    try {//from   www  .  jav a 2 s.co m
        MongoClient mongoClient = new MongoClient(mdlocalhost, mdport);
        DB db = mongoClient.getDB(tmtextdb);
        boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray());
        DBCollection collection = db.getCollection("package");
        DBCursor cursor = collection.find();
        BasicDBObject document = new BasicDBObject();
        document.put("MySQLid", id);
        document.put("FQN", FQN);
        document.put("name", name);
        document.put("container", container);
        collection.insert(document);

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java

License:Open Source License

public static void Deepmodeldocumentinsert(int id, int p_id, String FQN, String name, String container) {
    mdlocalhost = Login.tmtexthostx();/*from  w w  w . j  av  a  2s .  c o  m*/
    mdport = Integer.valueOf(Login.tmtextportx());
    tmtextdb = Login.tmtextdbx();
    tmtextuser = Login.tmtextuserx();
    tmtextpassword = Login.tmtextpasswordx();
    try {

        clearmongodb();
        MongoClient mongoClient = new MongoClient(mdlocalhost, mdport);
        DB db = mongoClient.getDB(tmtextdb);
        boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray());
        DBCollection collection = db.getCollection("deepmodel");
        DBCursor cursor = collection.find();
        BasicDBObject document = new BasicDBObject();
        document.put("MySQLid", id);
        document.put("p_id", p_id);
        document.put("FQN", FQN);
        document.put("name", name);
        document.put("container", container);
        collection.insert(document);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java

License:Open Source License

public static void Leveldocumentinsert(int id, String FQN, String name, String container, int number) {
    try {//from  w w  w  .j a  va 2 s  .  c  o m

        MongoClient mongoClient = new MongoClient(mdlocalhost, mdport);
        DB db = mongoClient.getDB(tmtextdb);
        boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray());
        DBCollection collection = db.getCollection("level");
        DBCursor cursor = collection.find();
        BasicDBObject document = new BasicDBObject();
        document.put("MySQLid", id);
        document.put("FQN", FQN);
        document.put("name", name);
        document.put("container", container);
        document.put("number", number);
        collection.insert(document);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java

License:Open Source License

public static void Entitydocumentinsert(int id, String FQN, String name, String container, int potincy,
        String directtype)//from ww w.jav  a2s  . c o m

{
    try {

        MongoClient mongoClient = new MongoClient(mdlocalhost, mdport);
        DB db = mongoClient.getDB(tmtextdb);
        boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray());

        DBCollection collection = db.getCollection("entity");
        DBCursor cursor = collection.find();
        BasicDBObject document = new BasicDBObject();
        document.put("MySQLid", id);
        document.put("name", name);
        document.put("FQN", FQN);
        document.put("container", container);
        document.put("potincy", potincy);
        collection.insert(document);

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java

License:Open Source License

public static void Attributedocumentinsert(int id, String FQN, String name, String container, String duribility,
        String type, String value, String mutability) {
    try {//  ww  w. j  a  v  a 2 s .  c  o m

        MongoClient mongoClient = new MongoClient(mdlocalhost, mdport);
        DB db = mongoClient.getDB(tmtextdb);
        boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray());
        DBCollection collection = db.getCollection("attribute");
        DBCursor cursor = collection.find();
        BasicDBObject document = new BasicDBObject();
        document.put("MySQLid", id);
        document.put("FQN", FQN);
        document.put("name", name);
        document.put("container", container);
        document.put("duribility", duribility);
        document.put("type", type);
        document.put("duribility", duribility);
        document.put("value", value);
        document.put("duribility", duribility);
        document.put("mutability", mutability);
        collection.insert(document);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java

License:Open Source License

public static void Methoddocumentinsert(int id, String FQN, String name, String container, String duribility,
        String signature, String body) {
    try {//from  w w w  .  j  a  v a2  s.  c o  m

        MongoClient mongoClient = new MongoClient(mdlocalhost, mdport);
        DB db = mongoClient.getDB(tmtextdb);
        boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray());
        DBCollection collection = db.getCollection("method");
        DBCursor cursor = collection.find();
        BasicDBObject document = new BasicDBObject();
        document.put("MySQLid", id);
        document.put("FQN", FQN);
        document.put("name", name);
        document.put("container", container);
        document.put("duribility", duribility);
        document.put("signature", signature);
        if (body == null) {
            body = "null";

        }
        document.put("body", body);
        collection.insert(document);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java

License:Open Source License

public static void Binarydocumentinsert(int id, String FQN, String name, String container, int potency,
        String directtype, String label, String participant1, String participant2, String roleName1,
        String roleName2, String lower1, String lower2, String upper1, String upper2, String navigalbeTo1,
        String navigableTo2) {/*from  w ww. j  a v a2s . c  o  m*/
    try {

        MongoClient mongoClient = new MongoClient(mdlocalhost, mdport);
        DB db = mongoClient.getDB(tmtextdb);
        boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray());
        DBCollection collection = db.getCollection("binaryconnection");
        DBCursor cursor = collection.find();
        BasicDBObject document = new BasicDBObject();
        document.put("MySQLid", id);
        document.put("FQN", FQN);
        document.put("name", name);
        document.put("potincy", potency);
        document.put("container", container);
        //               document.put("directtype", directtype);
        document.put("label", label);
        document.put("participant1", participant1);
        document.put("participant2", participant2);
        document.put("roleName1", roleName1);
        document.put("roleName2", roleName2);
        document.put("lower1", lower1);
        document.put("lower2", lower2);
        document.put("upper1", upper1);
        document.put("upper2", upper2);
        document.put("navigalbeTo1", navigalbeTo1);
        document.put("navigableTo2", navigableTo2);
        collection.insert(document);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}