Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2970

Unexpected error when using $ in field names during a $set

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Unknown Unknown
    • None
    • None
    • None
    • None
    • Hide

      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?

      Show
      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)
      	}
      }
      

      Attachments

        Activity

          People

            matt.dale@mongodb.com Matt Dale
            bhallion@ohbibi.com Benjamin Hallion
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: