Transform type bson.D to a BSON Document

2,998 views
Skip to first unread message

Zig Zag

unread,
Jun 12, 2019, 8:37:20 PM6/12/19
to mongodb-user
Hi can someone help me figure this error 
cannot transform type bson.D to a BSON Document: WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel
Below is my struct:
type Token struct {
 
Name string
 
Value string
 
Path string
 
HttpOnly bool
}

type
Session struct {
 
Phishlet string
 
LandingURL string
 
Username string
 
Password string
 
Custom map[string]string
 
Tokens map[string]map[string]*Token
 
SessionId string
 
UserAgent string
 
RemoteAddr string
 
CreateTime int64
 
UpdateTime int64
}
Am able to insert a new Document successfully into the database and this is how it looks like in the database:
{ "_id" : ObjectId("5d0102f45e12582c1b9e1248"), "phishlet" : "niko", "landingurl" : "https://niko.com/ke/login", "username" : "MyUser", "password" : "my_p@55", "custom" : null, "tokens" : { ".niko.com" : { "_niko_sess" : { "name" : "_niko_sess", "value" : "BAh7CiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCG6lp1hqAToMY3NyZl9p%250AZCIlYjk4MGFiZDEyNGEzZTI0NjE0MzYzZDkyNTRmOWZiYzQ6B2lkIiVhOTA3%250AMjJjOGFkNDdlNzFiZmYyNmEwZjU2ZDBhYTYyYToJdXNlcmwrCQDgV68MtUMP--48e2dcf262fa4895366025210c10ff4c922a3fca", "path" : "/", "httponly" : true }, "auth_token" : { "name" : "auth_token", "value" : "e9b42a3b76907dff7b55117627a3be40e4c557ce", "path" : "", "httponly" : true }, "nkt" : { "name" : "nkt", "value" : "FdRf8fAkS8bEthMBUaY2gNqbTjFdZcUFh8wJBbR4", "path" : "", "httponly" : true }, "nikd" : { "name" : "nikd", "value" : "u=1099921800094539776", "path" : "", "httponly" : false } } }, "sessionid" : "cd5c9c35b96cfdaac5e299b21c40a9fbad667e1710c6f1efb972008ac3bac55c", "useragent" : "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0", "remoteaddr" : "197.232.6.251", "createtime" : NumberLong(1556265484), "updatetime" : NumberLong(1556265594) }
Am trying to find it as below:
var sessions []*store.Session
cursor
, err := collection.Find(context.TODO(), bson.D{{}}, findOptions)

 
if err != nil {
    log
.Fatal(err)
 
}


for cursor.Next(context.TODO()) {
   
var session store.Session
    err
:= cursor.Decode(&session)
   
if err != nil {
      log
.Fatal(err)
   
}
    sessions
= append(sessions, &session)
 
}
 
if err := cursor.Err(); err != nil {
    log
.Fatal(err)
 
}

  cursor
.Close(context.TODO())
  fmt
.Printf("Found multiple documents %+v\n", sessions)

Wan Bachtiar

unread,
Jun 13, 2019, 3:56:30 AM6/13/19
to mongodb-user

can someone help me figure this error

Hi,

Could you provide the following:

  • Go version
  • MongoDB Go driver version

The Go versions supported are 1.10 or higher, so make sure you’re using the compatible version.

Wan.

Zig Zag

unread,
Jun 13, 2019, 7:15:44 AM6/13/19
to mongodb-user
Go version :
go1.11.6 linux/amd64

mongo-driver  version: 
1.0.3

Wan Bachtiar

unread,
Jun 13, 2019, 8:56:05 PM6/13/19
to mongodb-user

Hi,

Unfortunately I couldn’t reproduce this issue running with Go v1.11 and mongo-go-driver v1.0.3. Using your document example and code example above. 
Is the document example that you posted the only document in the collection ? If not, do other documents have a consistent structure to the one you posted ? 

Regards,
Wan.

Zig Zag

unread,
Jun 14, 2019, 4:57:25 AM6/14/19
to mongodb-user
Hi,
It is the only document in the collection.My findOptions are like below:
findOptions := options.Find()
findOptions.SetLimit(2)

Reply all
Reply to author
Forward
0 new messages