[GODRIVER-2970] Unexpected error when using $ in field names during a $set Created: 31/Aug/23  Updated: 06/Sep/23  Resolved: 06/Sep/23

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

Type: Bug Priority: Unknown
Reporter: Benjamin Hallion Assignee: Matt Dale
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

An unexpected error occurs in specific condition when trying to update a document with a somehow basic $set command.

If you try to $set an object property with an object containing innner property strating with `$` an error message reject the request.

Note that this error does not occurs when execute the exact same query from mongoSH.

Version of the mongodb golang driver: 1.12.1

How to Reproduce

Here is a short script to reproduce the issue:

 

package test
 
import (
	"context"
	"fmt"
	"testing"
 
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)
 
func TestBsonIssue(t *testing.T) {
	ctx := context.Background()
	clientOpts := options.Client().ApplyURI("mongodb://127.0.0.1/dev?retryWrites=true&w=majority")
	client, err := mongo.Connect(ctx, clientOpts)
	if err != nil {
		panic(fmt.Sprintf("failed to connect to mongo server: %s", err))
	}
	err = client.Ping(ctx, nil)
	if err != nil {
		panic(fmt.Sprintf("failed to ping mongo server: %s", err))
	}
	devDb := client.Database("dev")
	usersCol := devDb.Collection("Users")
 
	filter := bson.D{{"_id", primitive.NewObjectID()}}
	update := bson.A{
		bson.D{{"$set", bson.D{
			{"profile", bson.D{
				{"fieldName", "whatever"},
				{"$fieldName2", "someting"},
			}},
		}}},
	}
	updateOptions := options.Update().SetUpsert(true)
	_, err = usersCol.UpdateOne(ctx, filter, update, updateOptions)
	if err != nil {
		t.Fatalf("failed to update user: %s", err)
	}
}



 Comments   
Comment by Benjamin Hallion [ 06/Sep/23 ]

Yes ! thank you and have a nice day

Comment by Matt Dale [ 06/Sep/23 ]

bhallion@ohbibi.com thanks for the question!

I see you posted an answer to your question on the MongoDB community forum here:

Ok ! I found my issue, I need to remove the bson.A wrapped around my bson.D object. The bson.A cause the $set to be treated as an aggregation command, which does not support empty objects.

I'm glad you were able to resolve the issue! I'll close this. Please feel free to open a new ticket if you have more issues.

Comment by Benjamin Hallion [ 01/Sep/23 ]

Sorry, I forgot to add the error message returned by the provided code.
Here it is:

failed to update user: write exception: write errors:[Invalid $set :: caused by :: an expression specification must contain exactly one field, the name of the expression.Found 2 fields in { fieldName: "whatever", $$fieldName2: "someting" },while parsing object { profile: { fieldName: "whatever", $$fieldName2: "someting" } }]

Comment by PM Bot [ 31/Aug/23 ]

Hi bhallion@ohbibi.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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