I have a Document like this:
{
timestamp_hour: ISODate("xxx"),
userid: "xxx",
type: "xxx",
balances: {
1: {input: 100, output: 200},
2: {input: 200, output: 300},
500: {input: 5000, output: 5500},
...
}
}
I want to insert new pair of number: {input: xxx, output: yyy}
into balances
, if the timestamp_hour
and userid
both match. (It is more like an upsert
, I am told)
How the code would be? I am with Java 8 and org.apache.storm.core
1.1.
No upsert here. Just adding a new embedded field inside balances when the document is found.
MongoClient mc = new MongoClient();
MongoDatabase db = mc.getDatabase("db");
MongoCollection col = db.getCollection("col");
Bson query = Filters.and(Filters.eq("timestamp_hour", timestampHour), Filters.eq("userid", userId));
Document uDoc = new Document();
uDoc.put("input", xxx);
uDoc.put("output", yyy);
Bson update = Updates.set("balances." + number, uDoc);
col.updateOne(query, update);