[JAVA-3872] Unrecognized pipeline stage name: '$setOnInsert' Created: 27/Oct/20 Updated: 27/Oct/23 Resolved: 27/Oct/20 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | None |
| Affects Version/s: | 4.1.1 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Laurent Pellegrino | Assignee: | Unassigned |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Standalone MongoDB instance in version 4.4.1. The App that runs the Mongo client is using Java 15. |
||
| Description |
|
Using a standalone MongoDB instance in version 4.4.1 with a Java client that connects using the latest driver (`org.mongodb:mongodb-driver-sync:4.1.1`), I am getting an error when calling `findOneAndUpdate` with the $setOnInsert operator. Here is the query used:
The error:
If I get rid of the Updates.setOnInsert(...) call, then the update works but not as I would like. My purpose is to set some fields based on whether the document to update exists or not. Looking at the documentation, $setOnInsert should be supported: https://docs.mongodb.com/manual/reference/operator/update/#id1 I am wondering if I misuse the API or if that's a bug. |
| Comments |
| Comment by Jeffrey Yemin [ 27/Oct/20 ] | |||||||
|
Hi laurent.pellegrino@gmail.com Indeed, you're misusing the API, though I see now that it's quite easy to do. There are two main overloads of the findOneAndUpdate method. One that takes a normal update document, and one that takes an aggregation pipeline. You're using the latter when you need to use the former. Try this instead:
Update.combine is unfortunately a bit buried in the reference documentation. |