1

I have the following document on mongodb:

{
    "random": {
          "is_it_true": true
    }
}

And i want to read it and map it on the following struct on Golang:

type StructGolang struct {
    IsItTrue bool `bson:"random.is_it_true"`
}

But it isn't working. I'm getting an error when executing the Find command on the database.

1
  • Can you share the golang code, where you are performing the find operation? Also, share the error which you are getting. Commented May 8, 2021 at 4:59

1 Answer 1

1

Assuming your document is:

{
    "name": "some str-1",
    "random": {
          "isTrue": true
    }
}

You can model your document as one of the following (depends upon your usage):

type Random struct {
    IsTrue bool
}

type MyObj struct {
    Name string
    Random Random
}

Or,

type MyObj struct {
    Name string
    Random map[string]bool
}

Then query as follows:

var result MyObj
err := collection.FindOne(context.TODO(), bson.D{{}}).Decode(&result)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.