7

I am trying to find all users in a MongoDB collection that contains a username string in the friends array. I am using Golang with the mgo driver.

   type User struct {
    ...
        Friends        []string    `json: friends bson:"friends,omitempty"` 
    ...
    }

    ...
    // username is a string
    arr := []string{username}

    err := c.Find(bson.M{"friends": {"$in": arr}}).All(&users)
    ...

I get this error: http: panic serving [::1]:56358: assignment to entry in nil map

Any help would be greatly appreciated.

0

1 Answer 1

23

You are using "$in" wrong. You aren't initialising inner map. You are supposed to use it like this:

err := c.Find(bson.M{"friends": bson.M{"$in": arr}}).All(&users)
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that and now I get. http: panic serving [::1]:59541: assignment to entry in nil map
Never mind the issue was down the line in the HTTP response. You are correct that resolved the issue. Thank you for the help!
thanks, bson.M is much simpler to work than bson.D and bson.E

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.