[JAVA-2128] Accept Documents for setOnInsert Created: 29/Feb/16  Updated: 05/Feb/19  Resolved: 21/Nov/18

Status: Closed
Project: Java Driver
Component/s: Builders
Affects Version/s: 3.2.2
Fix Version/s: 3.10.0

Type: New Feature Priority: Major - P3
Reporter: Ben [X] Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Updates.setOnInsert just accepts (String, TItem) as arguments. I think it would be developer friendlier to accept a Document, too.

Add Updates.setOnInsert(Bson Items) as well for multiple fields that need to be set on insert.



 Comments   
Comment by Githook User [ 14/Jan/19 ]

Author:

{'username': 'rozza', 'email': 'ross.lawley@gmail.com', 'name': 'Ross Lawley'}

Message: Ensure notNull in Updates.setOnInsert

JAVA-2128
Branch: 3.10.x
https://github.com/mongodb/mongo-java-driver/commit/6ae9cd58c16826ac7abb37102d20456c433a3000

Comment by Githook User [ 14/Jan/19 ]

Author:

{'username': 'rozza', 'email': 'ross.lawley@gmail.com', 'name': 'Ross Lawley'}

Message: Extended Updates.setOnInsert helper.

It can now take a whole document.

JAVA-2128
Branch: 3.10.x
https://github.com/mongodb/mongo-java-driver/commit/c12de8e66b9477ce64c56d0a385e5e88a655c0f7

Comment by Githook User [ 21/Nov/18 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}

Message: Ensure notNull in Updates.setOnInsert

JAVA-2128
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/82f6ca98654204963bf7eeeb8ec0524bdf1e0906

Comment by Githook User [ 21/Nov/18 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}

Message: Extended Updates.setOnInsert helper.

It can now take a whole document.

JAVA-2128
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/01b94a5128c03a0b014110a674378f23073809cb

Comment by Ross Lawley [ 01/Mar/16 ]

Thanks for clarifying ChampS, sounds like it would be useful!

Comment by Ben [X] [ 01/Mar/16 ]

Hi Ross and thanks for your quick reply,

it's more like your second suggestion. I know I could use "new Document("$setOnInsert", myNewDoc)", but imho it also should work with the setOnInsert method. I think something like "Updates.setOnInsert(myNewDoc)" would be more java like.

So this bug report is not about solving a problem, but improving the java driver if needed.

best regards
Ben

Comment by Ross Lawley [ 29/Feb/16 ]

Hi ChampS,

The helper already accepts a document as the value for the key to be updated eg:

setOnInsert('subDocument', new Document("key", "value"))

Or was it more that you wanted to pass a document of all the keys and values to be set?
If thats the case then currently you can't use the helper but its easy to create the whole $setOnInsert document:

new Document("$setOnInsert", new Document("key", "value").append("key2", "value2"))

I hope that helps,

Ross

Generated at Thu Feb 08 08:56:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.