Morning everyone,
I hoping that i can seek some advice from this comunity…
Im trying to search accros fields in documents with an array of search terms to match values that im looking for using the $in model. But i cant seem to get this working…
This is what my test data looks like, it connects ok and inserts the data to the atlas DB multiple times with differing values like “test1211” etc:
quickstartDatabase := client.Database("quickstart")
podcastsCollection := quickstartDatabase.Collection("podcasts")
podcastResult, err := podcastsCollection.InsertOne(ctx, bson.D{
{"title", "The Polyglot Developer Podcast"},
{"author", "Nic Raboy"},
{"tags", bson.A{"development", "programming", "coding"}},
{"nested0",
bson.D{
{"nested1", bson.D{
{"val1", "test12"},
{"val2", "test212"},
},
},
}},
})
I have got it working using the $regex model for a single search term to find records with the value endng with 1, from my current data set it would return 3 records:
queryWord := "1$"
query := bson.M{"nested0.nested1.val1": bson.M{"$regex": queryWord, "$options": "im"}}
cursor, err := podcastsCollection.Find(ctx, query)
if err != nil {
log.Println(err)
}
var sites []bson.M
if err = cursor.All(ctx, &sites); err != nil {
log.Fatal(err)
}
for _, rec := range sites {
fmt.Println()
fmt.Println(rec)
}
My issue is with this search against the same data when trying to do a search with $in and an array of terms to find records with values ending in 1 or 2:
query := bson.A{"/1$/", "2$"}
filter := bson.D{
{
Key: "nested0.nested1.val1",
Value: bson.E{
Key: "$in",
Value: query,
},
},
}
cursor, err := podcastsCollection.Find(ctx, filter)
var sites []bson.M
if err = cursor.All(ctx, &sites); err != nil {
log.Fatal(err)
}
for _, rec := range sites {
fmt.Println()
fmt.Println(rec)
}
Is there something im missing here?
Thanks in advance