Search in sources :

Example 96 with Decimal128

use of org.bson.types.Decimal128 in project quickstart-database by youngzil.

the class Decimal128QuickTour method main.

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes an optional single argument for the connection string
 */
public static void main(final String[] args) {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = MongoClients.create();
    } else {
        mongoClient = MongoClients.create(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());
}
Also used : MongoClient(com.mongodb.client.MongoClient) Decimal128(org.bson.types.Decimal128) Document(org.bson.Document) BigDecimal(java.math.BigDecimal) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

Decimal128 (org.bson.types.Decimal128)96 ObjectId (org.bson.types.ObjectId)53 Test (org.junit.Test)48 Date (java.util.Date)47 BigDecimal (java.math.BigDecimal)25 AllTypes (io.realm.entities.AllTypes)22 UUID (java.util.UUID)18 UiThreadTest (androidx.test.annotation.UiThreadTest)12 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)11 AllJavaTypes (io.realm.entities.AllJavaTypes)10 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)10 Dog (io.realm.entities.Dog)9 List (java.util.List)9 BsonDocument (org.bson.BsonDocument)9 PrimaryKeyAsObjectId (io.realm.entities.PrimaryKeyAsObjectId)8 Document (org.bson.Document)8 JSONObject (org.json.JSONObject)8 MappedAllJavaTypes (io.realm.entities.MappedAllJavaTypes)7 BsonDecimal128 (org.bson.BsonDecimal128)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)6