Search code examples
mongodbgomongo-go

Doing a difficult mongoDB find


I need to find a document by an ID of a user which is in an array in that document, which looks like

{
  "uuid": "000-000-000whatever",
  users: 
  [
    {
      "id":"id1",
      "role":"role1"
    },
    {
      "id":"id2",
      "role":"role1"
    }
  ]
}

and while I know that for that i could in mongo do "users.id" as a filter, i don't have a clue on how to do it using bson from Go.

The smartest thing I could figure was something along the line of

bson.M{"users": bson.A{bson.M{"id":id}}}

but needless to say, it didn't work.


Solution

  • It's the "same" in Go too:

    bson.M{"users.id": id}