Spring Mongo Upsert nested document
I am using spring-mongodb .. Here is what I do
Intrest insertObj = new Insert();//initilize insert obj here ..Update args = new Update();args.addToSet("intrest",insertObj);Query query = new Query(Criteria.where("id").is("527242d584ae917d8bd75c7b"));// if u want to do upsert mongoOperation.findAndModify(query, args, FindAndModifyOptions.options().upsert(true), Post.class);//if u want to just updatemongoOperation.findAndModify(query, args, Post.class);
I think what you intend to do is an update. Upsert will modify your document matching the given query if not it will create a new document , where as update will only modify your document if found. here is the reference
I do not know about java, but all you need to do is $pushAll operator (I really hope you can find how to do this with java driver).
db.collection.update( {"_id" : ObjectId("527242d584ae917d8bd75c7b")}, { $pushAll: { intrest: [ { "userId" : ObjectId("526a587d84aed6f41cca10bc"), "timestamp" : ISODate("2013-10-31T11:45:25.256Z") }, { "userId" : ObjectId("526a587d84aed6f41cca10bc"), "timestamp" : ISODate("2013-11-31T11:55:25.256a") }] } });