Example usage for com.mongodb.client MongoCollection insertOne

List of usage examples for com.mongodb.client MongoCollection insertOne

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection insertOne.

Prototype

void insertOne(TDocument document);

Source Link

Document

Inserts the provided document.

Usage

From source file:org.jboss.as.test.compat.nosql.mongodb.jaxrs.ClientResource.java

License:Open Source License

@GET
@Produces({ "text/plain" })
public String get() {

    MongoCollection collection = null;
    Document query = null;//from   ww  w  . jav  a  2s  .  c  o m
    try {
        collection = database.getCollection("company");
        String companyName = "Acme products";
        JsonObject object = Json.createObjectBuilder().add("companyName", companyName)
                .add("street", "999 Flow Lane").add("city", "Indiville").add("_id", companyName).build();
        Document document = Document.parse(object.toString());
        collection.insertOne(document);
        query = new Document("_id", companyName);
        FindIterable cursor = collection.find(query);
        Object dbObject = cursor.first();
        return dbObject.toString();
    } finally {
        if (query != null) {
            collection.drop();
        }
    }
}

From source file:org.jboss.as.test.compat.nosql.mongodb.modules.StatefulTestBean.java

License:Open Source License

public String addUserComment() {
    MongoCollection collection = null;
    Document query = null;/*from w  w w  .java 2s  .com*/
    try {
        // add a comment from user Melanie
        String who = "Melanie";
        Document comment = new Document("_id", who).append("name", who)
                .append("address",
                        new BasicDBObject("street", "123 Main Street").append("city", "Fastville")
                                .append("state", "MA").append("zip", 18180))
                .append("comment", "MongoDB Is Web Scale");
        // save the comment
        collection = database.getCollection("comments");
        collection.insertOne(comment);

        // look up the comment from Melanie
        query = new Document("_id", who);
        FindIterable cursor = collection.find(query);
        Object userComment = cursor.first();
        return userComment.toString();
    } finally {
        collection.drop();
    }
}

From source file:org.jboss.as.test.compat.nosql.mongodb.modules.StatefulTestBean.java

License:Open Source License

public String addProduct() {
    MongoCollection collection = null;
    Document query = null;// www  .j  ava 2s .  co m
    try {
        collection = injectedDatabase.getCollection("company");
        String companyName = "Acme products";
        JsonObject object = Json.createObjectBuilder().add("companyName", companyName)
                .add("street", "999 Flow Lane").add("city", "Indiville").add("_id", companyName).build();
        Document document = Document.parse(object.toString());
        collection.insertOne(document);
        query = new Document("_id", companyName);
        FindIterable cursor = collection.find(query);
        Object dbObject = cursor.first();
        return dbObject.toString();
    } finally {
        if (query != null) {
            collection.drop();
        }
    }
}

From source file:org.jboss.as.test.compat.nosql.mongodb.StatefulTestBean.java

License:Open Source License

public String addUserComment() {
    MongoCollection collection = null;
    Document query = null;//from   ww w.ja  v  a2 s  .c om
    try {
        // add a comment from user Melanie
        String who = "Melanie";
        Document comment = new Document("_id", who).append("name", who)
                .append("address",
                        new BasicDBObject("street", "123 Main Street").append("city", "Fastville")
                                .append("state", "MA").append("zip", 18180))
                .append("comment",
                        "I really love your new website but I have a lot of questions about using NoSQL versus a traditional RDBMS.  "
                                + "I would like to sign up for your 'MongoDB Is Web Scale' training session.");
        // save the comment
        collection = database.getCollection("comments");
        collection.insertOne(comment);

        // look up the comment from Melanie
        query = new Document("_id", who);
        FindIterable cursor = collection.find(query);
        Object userComment = cursor.first();
        return userComment.toString();
    } finally {
        collection.drop();
    }
}

From source file:org.jboss.as.test.compat.nosql.mongodb.StatefulTestBean.java

License:Open Source License

public String addProduct() {
    MongoCollection collection = null;
    Document query = null;// w  w  w.j ava 2 s .c o  m
    try {
        collection = database.getCollection("company");
        String companyName = "Acme products";
        JsonObject object = Json.createObjectBuilder().add("companyName", companyName)
                .add("street", "999 Flow Lane").add("city", "Indiville").add("_id", companyName).build();
        Document document = Document.parse(object.toString());
        collection.insertOne(document);
        query = new Document("_id", companyName);
        FindIterable cursor = collection.find(query);
        Object dbObject = cursor.first();
        return dbObject.toString();
    } finally {
        if (query != null) {
            collection.drop();
        }
    }
}

From source file:org.jboss.as.test.compat.nosql.multiple.StatefulTestBean.java

License:Open Source License

public String addSalesComment() {
    MongoCollection collection = null;
    Document query = null;//  ww  w  .j  ava 2 s  .c  o m
    try {
        // add a comment from Sales department
        String who = "InsideSalesTeam";
        Document comment = new Document("_id", who).append("name", who)
                .append("address",
                        new BasicDBObject("street", "inside sales").append("city", "internal")
                                .append("state", "internal").append("zip", 99999))
                .append("comment", "Need to sell sell sell");
        // save the comment
        collection = database.getCollection("comments");
        collection.insertOne(comment);

        query = new Document("_id", who);
        FindIterable cursor = collection.find(query);
        Object userComment = cursor.first();
        return userComment.toString();
    } finally {
        collection.drop();
    }
}

From source file:org.jnosql.diana.mongodb.document.MongoDBDocumentCollectionManager.java

License:Open Source License

@Override
public DocumentEntity insert(DocumentEntity entity) {
    String collectionName = entity.getName();
    MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
    Document document = getDocument(entity);
    collection.insertOne(document);
    boolean hasNotId = entity.getDocuments().stream().map(document1 -> document1.getName())
            .noneMatch(k -> k.equals(ID_FIELD));
    if (hasNotId) {
        entity.add(Documents.of(ID_FIELD, document.get(ID_FIELD)));
    }/* w w w .  ja v a  2 s .c o  m*/
    return entity;
}

From source file:org.nuxeo.directory.mongodb.MongoDBReference.java

License:Apache License

protected void initializeSession(MongoDBSession session) {
    // fake schema for DirectoryCSVLoader.loadData
    SchemaImpl schema = new SchemaImpl(collection, null);
    schema.addField(sourceField, StringType.INSTANCE, null, 0, Collections.emptySet());
    schema.addField(targetField, StringType.INSTANCE, null, 0, Collections.emptySet());

    Consumer<Map<String, Object>> loader = map -> {
        Document doc = MongoDBSerializationHelper.fieldMapToBson(map);
        MongoCollection<Document> coll = session.getCollection(collection);
        if (coll.count(doc) == 0) {
            coll.insertOne(doc);
        }//from w ww .  java  2 s  .c o m
    };
    DirectoryCSVLoader.loadData(dataFileName, BaseDirectoryDescriptor.DEFAULT_DATA_FILE_CHARACTER_SEPARATOR,
            schema, loader);
}

From source file:org.pac4j.mongo.test.tools.MongoServer.java

License:Apache License

public void start(final int port) {
    MongodStarter starter = MongodStarter.getDefaultInstance();

    try {// ww w.j  a  v  a2s.c  o  m
        IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION)
                .net(new Net(port, Network.localhostIsIPv6())).build();

        mongodExecutable = starter.prepare(mongodConfig);
        MongodProcess mongod = mongodExecutable.start();

        // populate
        final MongoClient mongo = new MongoClient("localhost", port);
        final MongoDatabase db = mongo.getDatabase("users");
        db.createCollection("users");
        final MongoCollection<Document> collection = db.getCollection("users");
        final PasswordEncoder encoder = new BasicSaltedSha512PasswordEncoder(SALT);
        final String password = encoder.encode(PASSWORD);
        Map<String, Object> properties1 = new HashMap<>();
        properties1.put(USERNAME, GOOD_USERNAME);
        properties1.put(PASSWORD, password);
        properties1.put(FIRSTNAME, FIRSTNAME_VALUE);
        collection.insertOne(new Document(properties1));
        Map<String, Object> properties2 = new HashMap<>();
        properties2.put(USERNAME, MULTIPLE_USERNAME);
        properties2.put(PASSWORD, password);
        collection.insertOne(new Document(properties2));
        Map<String, Object> properties3 = new HashMap<>();
        properties3.put(USERNAME, MULTIPLE_USERNAME);
        properties3.put(PASSWORD, password);
        collection.insertOne(new Document(properties3));

    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.piotr.apollo.service.AnswerService.java

public void addAnswer(Answer answer, ObjectId questionId) {
    MongoCollection collection = db.getCollection(answerCollection);

    Document objInsert = new Document();

    objInsert.put("answer", answer.getAnswer());
    objInsert.put("correct", answer.getCorrect());
    objInsert.put("question_id", questionId);

    collection.insertOne(objInsert);
}