Create dynamic nested map[string]interface{}

Hi to all

I need to build dinamyc mongoDB update query based on request body payload.

Source Code Example

https://play.golang.org/p/wBQZrmOm2cH

What i need to do is create a map[string]map[string]interface{}
with different values for same key without overriding, how i can do?

The code in your Play is quite complex. Can you trim it down to a Short, Self Contained, Correct (Compilable), Example? This will make it easier for us to help you.

Updated example with trimmed body request

https://play.golang.org/p/-zu4Eibk9lA

Hi, Gabriele,

When you’re looping over the keys in the m map in PrepareBsonM, you’re creating new maps and overwriting previous ones you set in your set map under the "$set" key, this way, only the last one “wins.”

Is PrepareBsonM's output supposed to look like query?

Hi @skillian Yes the output of PrepareBsonM should look like query.
How i can achieve the desired output?

How i can achieve the desired output?

i have tried to create a slice of interface

map[string]map[string][]interface{}

But doesen’t work it returns an error beacuse m[i] is an interface and not a slice of interface

Solved adding check to create nested map[string] only if not already created

https://play.golang.org/p/sW7QBbWDV6u

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.