Reputation: 6158
I am having following mongo query which is executed in mongo shell.
db.test.update({
uuid: "160597270101684",
sessionId: "160597270101684.1"
}, {
$setOnInsert: {
stamps: {
currentVisit: "1377500985",
lastVisit: "1377500985"
}
},
$push:{
visits: {
page: "google.com",
method: "GET"
}
}
}, { upsert:true })
Because i am new to java, I am little bit confused to create the basicDBObject.
I had tried like this for sample
BasicDBObject doc = new BasicDBObject("uuid",1).append("session",2);
BasicDBObject upsertion = new BasicDBObject("upsert",true);
collection.update(doc,upsertion);
But its not working.
Any help will be great.
Upvotes: 1
Views: 1235
Reputation: 17084
The upsert
option isn't specified with a DBObject
but with a third argument to DBCollection.update
public WriteResult update(DBObject q, DBObject o, boolean upsert, boolean multi)
You'll need to form a DBObject for update by appending $setOnInsert
, $push
, stamps
and visits
.
BasicDBObject update = new BasicDBObject();
BasicDBObject stamps = new BasicDBObject();
stamps.append("currentVisit", "1377500985").append("lastVisit", "1377500985");
BasicDBObject visits = new BasicDBObject();
update.append("$setOnInsert", stamps).append("$push", visits);
collection.update(doc, update, true);
Upvotes: 3