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:swm.project.loadDataToDb.LoadDataToDb.java

private void addMovieOtherFeatures(int id, String year, String director, String[] genre,
        MongoCollection<Document> movieOtherCollection) {
    Document d = new Document();
    d.put("_id", id);
    d.put(Consts.MOVIE_YEAR, year);//w  ww .ja  v  a2 s .c o  m
    d.put(Consts.MOVIE_DIRECTOR, director);
    String cert = genre[0];
    //System.out.println(cert);
    d.put(Consts.MOVIE_CERT, cert);
    movieOtherCollection.insertOne(d);
}

From source file:swm.project.loadDataToDb.LoadDataToDb.java

private void addMovieGenre(int id, String[] genre, MongoCollection<Document> movieGenreCollection) {
    Document d = new Document();
    d.put("_id", id);
    String gen = "";
    for (int i = 1; i < genre.length; i++)
        gen += genre[i];/*from   w  w w.  j a va  2s  .  c o  m*/
    //System.out.println(gen);
    d.put(Consts.MOVIE_GENRE_DATA, gen);
    movieGenreCollection.insertOne(d);
}

From source file:swm.project.loadDataToDb.LoadDataToUserDb.java

private void addUserAge(int id, int age, MongoCollection<Document> userAgeCollection) {
    Document d = new Document();
    d.put("_id", id);
    d.put(Consts.USER_AGE, age);/*from w  w w.  jav  a 2 s  .c o  m*/
    userAgeCollection.insertOne(d);
}

From source file:swm.project.loadDataToDb.LoadDataToUserDb.java

private void addUserGender(int id, String gender, MongoCollection<Document> userGenderCollection) {
    Document d = new Document();
    d.put("_id", id);
    d.put(Consts.USER_GENDER, gender);//ww w. jav a  2 s.c  o m
    userGenderCollection.insertOne(d);
}

From source file:swm.project.loadDataToDb.LoadDataToUserDb.java

private void addUserOccupation(int id, String occupation, MongoCollection<Document> userOccupationCollection) {
    Document d = new Document();
    d.put("_id", id);
    d.put(Consts.USER_OCCUPATION, occupation);
    userOccupationCollection.insertOne(d);
}

From source file:swm.project.loadDataToDb.LoadDataToUserDb.java

private void addUserZip(int id, String zip, MongoCollection<Document> userZipCollection) {
    Document d = new Document();
    d.put("_id", id);
    d.put(Consts.USER_ZIP, zip);//  w  ww  .jav  a  2s .c om
    userZipCollection.insertOne(d);
}

From source file:Tests.AddElementToCollection.java

public void addToCollection(String jsonString) {
    MongoClient mongoClient = new MongoClient(new ServerAddress(),
            Arrays.asList(MongoCredential.createCredential("admin", "test", "password".toCharArray())));
    try {//from ww  w .  jav a 2 s  . com
        for (String databaseName : mongoClient.listDatabaseNames()) {
            System.out.println("Database: " + databaseName);
        }
        MongoDatabase db = mongoClient.getDatabase("test");
        MongoCollection coll = db.getCollection("test2");

        Document doc = Document.parse(jsonString);
        coll.insertOne(doc);
    } finally {
        mongoClient.close();
    }
}

From source file:tour.ClientSideEncryptionAutoEncryptionSettingsTour.java

License:Apache License

/**
 * Run this main method to see the output of this quick example.
 *
 * Requires the mongodb-crypt library in the class path and mongocryptd on the system path.
 *
 * @param args ignored args/*from   www.j a  v  a 2 s .  co m*/
 */
public static void main(final String[] args) {

    // This would have to be the same master key as was used to create the encryption key
    final byte[] localMasterKey = new byte[96];
    new SecureRandom().nextBytes(localMasterKey);

    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {
        {
            put("local", new HashMap<String, Object>() {
                {
                    put("key", localMasterKey);
                }
            });
        }
    };

    String keyVaultNamespace = "admin.datakeys";
    ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder()
            .keyVaultMongoClientSettings(MongoClientSettings.builder()
                    .applyConnectionString(new ConnectionString("mongodb://localhost")).build())
            .keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).build();

    ClientEncryption clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
    BsonBinary dataKeyId = clientEncryption.createDataKey("local", new DataKeyOptions());
    final String base64DataKeyId = Base64.getEncoder().encodeToString(dataKeyId.getData());

    final String dbName = "test";
    final String collName = "coll";
    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder()
            .keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders)
            .schemaMap(new HashMap<String, BsonDocument>() {
                {
                    put(dbName + "." + collName,
                            // Need a schema that references the new data key
                            BsonDocument.parse("{" + "  properties: {" + "    encryptedField: {"
                                    + "      encrypt: {" + "        keyId: [{" + "          \"$binary\": {"
                                    + "            \"base64\": \"" + base64DataKeyId + "\","
                                    + "            \"subType\": \"04\"" + "          }" + "        }],"
                                    + "        bsonType: \"string\","
                                    + "        algorithm: \"AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic\""
                                    + "      }" + "    }" + "  }," + "  \"bsonType\": \"object\"" + "}"));
                }
            }).build();

    MongoClientSettings clientSettings = MongoClientSettings.builder()
            .autoEncryptionSettings(autoEncryptionSettings).build();

    MongoClient mongoClient = MongoClients.create(clientSettings);
    MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("coll");
    collection.drop(); // Clear old data

    collection.insertOne(new Document("encryptedField", "123456789"));

    System.out.println(collection.find().first().toJson());

    // release resources
    mongoClient.close();
}

From source file:tour.ClientSideEncryptionSimpleTour.java

License:Apache License

/**
 * Run this main method to see the output of this quick example.
 *
 * Requires the mongodb-crypt library in the class path and mongocryptd on the system path.
 * Assumes the schema has already been created in MongoDB.
 *
 * @param args ignored args//from  ww  w  . jav a 2s  . c om
 */
public static void main(final String[] args) {

    // This would have to be the same master key as was used to create the encryption key
    final byte[] localMasterKey = new byte[96];
    new SecureRandom().nextBytes(localMasterKey);

    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {
        {
            put("local", new HashMap<String, Object>() {
                {
                    put("key", localMasterKey);
                }
            });
        }
    };

    String keyVaultNamespace = "admin.datakeys";

    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder()
            .keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).build();

    MongoClientSettings clientSettings = MongoClientSettings.builder()
            .autoEncryptionSettings(autoEncryptionSettings).build();

    MongoClient mongoClient = MongoClients.create(clientSettings);
    MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("coll");
    collection.drop(); // Clear old data

    collection.insertOne(new Document("encryptedField", "123456789"));

    System.out.println(collection.find().first().toJson());

    // release resources
    mongoClient.close();
}

From source file:tour.Decimal128QuickTour.java

License:Apache License

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes an optional single argument for the connection string
 *///  w  w w  .j  a v  a2 s  .com
public static void main(final String[] args) {
    MongoClient mongoClient;

    if (args.length == 0) {
        // connect to the local database server
        mongoClient = new MongoClient();
    } else {
        mongoClient = new MongoClient(new MongoClientURI(args[0]));
    }

    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");

    // get a handle to the "test" collection
    MongoCollection<Document> collection = database.getCollection("test");

    // drop all the data in it
    collection.drop();

    // make a document and insert it
    Document doc = new Document("name", "MongoDB").append("amount1", Decimal128.parse(".10"))
            .append("amount2", new Decimal128(42L)).append("amount3", new Decimal128(new BigDecimal(".200")));

    collection.insertOne(doc);

    Document first = collection.find().filter(Filters.eq("amount1", new Decimal128(new BigDecimal(".10"))))
            .first();

    Decimal128 amount3 = (Decimal128) first.get("amount3");
    BigDecimal amount2AsBigDecimal = amount3.bigDecimalValue();

    System.out.println(amount3.toString());
    System.out.println(amount2AsBigDecimal.toString());

}