[GODRIVER-880] Cannot query when having a regular expression in MongoDB Document Created: 15/Mar/19  Updated: 28/Oct/23  Resolved: 26/Mar/19

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 1.0.0-rc2
Fix Version/s: 1.0.1

Type: Bug Priority: Major - P3
Reporter: Huynh Thanh Thao Assignee: Kristofer Brandow (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
is caused by GODRIVER-893 bsonrw.valueReader skip does not prop... Closed

 Description   

I have a document and an object below:

 

{
 "_id" : ObjectId("5c7642f49bd4e700332e82ea"),
 "name" : "Favorite",
 "reg" : /^L0F1[a-zA-Z0-9]*$/m
}

 

type Test struct {
  ID   primitive.ObjectID `bson:"_id,omitempty" json:"id"`
  Name string             `bson:"name,omitempty" json:"name"`
}

 

When I unmarshal the document to my object, I got attempted to read bytes of unknown BSON type invalid error.

 



 Comments   
Comment by Githook User [ 26/Mar/19 ]

Author:

{'email': 'kris@mongodb.com', 'name': 'Kris Brandow', 'username': 'skriptble'}

Message: Fix length determination for regex

GODRIVER-893
GODRIVER-880

Change-Id: I833df440fd652ec2e079244d91edaf1b2530053d
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/03f71cffc6c1b6c1de8b5c6d988d04916f4fbf7d

Comment by Huynh Thanh Thao [ 22/Mar/19 ]

Thank you so much!

Comment by Kristofer Brandow (Inactive) [ 21/Mar/19 ]

Hello thaoht,

I've been able to reproduce the bug and have made GODRIVER-893 to track the fix.

--Kris

Comment by Huynh Thanh Thao [ 18/Mar/19 ]

Hello Kris Brandow,

This is the document:

{ 
    "_id" : ObjectId("5c7642f49bd4e700332e82ea"), 
    "name" : "Favorite",
    "reg" : /^L0F1[a-zA-Z0-9]*$/m
}

Comment by Kristofer Brandow (Inactive) [ 18/Mar/19 ]

Hi thaoht,

Can add the inserting of the document to the example? Since I don't have any documents in the portfolio_dev.Test collection I'm not getting any documents back.

--Kris

Comment by Huynh Thanh Thao [ 18/Mar/19 ]

Hello Kris Brandow,

  • The version of the driver: v1.0.0
  • The version of Go: go1.12 darwin/amd64
  • MongoDB version: v4.0.6
  • The full example:

 

package main
 
import (
   "context"
   "log"
   "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 main() {
   type Test struct {
      ID   primitive.ObjectID `bson:"_id,omitempty" json:"id"`
      Name string             `bson:"name,omitempty" json:"name"`
   }
 
   client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017/"))
   if err != nil {
      log.Fatal(err)
   }
 
   if err := client.Connect(context.Background()); err != nil {
      log.Fatal(err)
   }
   collection := client.Database("portfolio_dev").Collection("Test")
 
   result := Test{}
   err = collection.FindOne(context.Background(), bson.D{{Key: "name", Value: "Favorite"}}).Decode(&result)
   if err != nil {
      log.Fatal(err)
   }
}

  • I got an error below:

attempted to read bytes of unknown BSON type invalid

Comment by Kristofer Brandow (Inactive) [ 15/Mar/19 ]

Hi thaoht,

Can you please provide the version of the driver, version of Go, and a complete example of this problem?

--Kris

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