Create dynamic nested map[string]interface{}


(Gabriele Bassi) #1

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?


(Lutz Horn) #2

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.


(Gabriele Bassi) #3

Updated example with trimmed body request

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


(Sean Killian) #4

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?


(Gabriele Bassi) #5

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


(Gabriele Bassi) #6

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

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