[GODRIVER-2425] use nil slice in `$in` will cause "(BadValue) $in needs an array" Created: 17/May/22 Updated: 27/Oct/23 Resolved: 23/Jun/22 |
|
| Status: | Closed |
| Project: | Go Driver |
| Component/s: | None |
| Affects Version/s: | 1.9.1 |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Minor - P4 |
| Reporter: | yahui An | Assignee: | Benji Rewis (Inactive) |
| Resolution: | Gone away | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Description |
|
go version go1.18 windows/amd64 go.mongodb.org/mongo-driver v1.9.1 MongoDB server version: 5.0.2
the result is ``panic: (BadValue) $in needs an array`` now i have to check it before find
can someone help me? thank you very much |
| Comments |
| Comment by PM Bot [ 23/Jun/22 ] |
|
There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information. |
| Comment by Benji Rewis (Inactive) [ 08/Jun/22 ] |
|
Thanks for your report, anyahui741600@outlook.com! Apologies for the delay, the Go driver team has been preparing for MongoDB World, which is happening this week. As you've noted in your example, the server is reporting that $in needs to be an array. This is because of how we Marshal slices in the Go driver. The "empty" slice has a value (is non-nil), and therefore Marshals to an empty BSON array. The nil slice has a nil value, and therefore Marshal to BSON null. The server errors for the latter, as $in does not accept BSON null and expects a BSON array. As I mentioned in GODRIVER-2137, I see an argument to have nil slices in the Go driver Marshal to empty BSON arrays instead of BSON null. However, changing the driver to do so would break users' existing code that relies on the fact that something like var list []string is Marshaled as BSON null. We could not make a change like that until the next major release of the driver. I've marked this ticket as related to GODRIVER-2137. As far as a workaround for you, I believe you could make use of the mgocompat registry instead of the default BSON registry. Using the mgocompat registry makes the BSON library follow behavior similar to the mgo driver (an old, community-built driver) that does encode nil slices as BSON null. Let me know if that works. |