0

I am trying to implement a autocomplete search in golang but I keep getting an error with the syntax

contactsCollection := c.DB.Database("XYZ").Collection("contacts")

    result, err := contactsCollection.aggregate([{
            "$search": {
                "autocomplete": {
                    "query": searchQuery,
                    "path": "email",
                    "tokenOrder": "any"
                }
            }
        }
    ])

searchQuery is the input coming in and email is the field upon which I want to do the autocomplete. I am assuming that I would be able to return result.

What am I missing here?

1 Answer 1

1

Check out some tutorials or docs on how to use the MongoDB client in Go. You will need to use the bson package to write queries.

cursor, err := contactsCollection.Aggregate(ctx, bson.A{
    bson.M{"$search": bson.M{"autocomplete": bson.M{
        "query":      searchQuery,
        "path":       "email",
        "tokenOrder": "any",
    }}},
})
if err != nil {
    panic(err)
}
var results []MyDocument
err = cursor.All(ctx, &results)
if err != nil {
    panic(err)
}
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.