[GODRIVER-1796] Upsert with $inc: write errors: '$inc' is not valid for storage Created: 17/Nov/20  Updated: 27/Oct/23  Resolved: 19/Nov/20

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: 1.4.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Rubi N/A Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 20.04
go version go1.15.3 linux/amd64



 Description   

 

opt := options.Update() opt.SetUpsert(true) 
upd, err := mgdb.Collection("users_usage").UpdateOne(context.TODO(), filter, bson.M{"$set": update}, opt)

given this code with update data :

// code placeholder
update := bson.M{"user_id": userID,"$inc": bson.M{"usage_total": 1}}

resulting in error :

multiple write errors: [{write errors: [{The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage.}]}, {<nil>}]

but when i disable the upsert, the error gone



 Comments   
Comment by Kevin Albertson [ 19/Nov/20 ]

Hello jihantoro8@gmail.com! Thank you for your interest in MongoDB's Go driver.

I do not believe this is a Go driver issue. This is a current limitation in MongoDB servers. Top-level fields cannot start with a "$" character.

See https://docs.mongodb.com/manual/reference/limits/#naming-restrictions for more information.

For example, you can reproduce the same error in the shell by running a raw update command:

> db.runCommand( {update: "coll", updates: [ { q: {}, u: { $set: { user_id: 123, $inc: { usage_total: 1 } } }, upsert: true } ] } )

Gives back an error containing "The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage."

but when i disable the upsert, the error gone

That makes sense. If the filter does not match anything, and upsert is false, then nothing is updated or inserted.

Since this isn't a bug or feature request we're closing the issue. For further assistance please create a post in our community forum here 

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