Searching for values in documents with multiple values with $in

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

Hello @Stuart_Packham, welcome to the forum!

You can perform the search using $regex and the regular expression boolean operator or, the | (See: Regular expression syntax cheatsheet - JavaScript | MDN). This you can use without specifying the $in.

Also, see similar Stack Overflow post with an answer using $in:

thanks @Prasad_Saya, i will take a look at that.

But there seems to be an issue the the $in statement i have, does the golang implementation for it look correct? as i cant seem to find an example

Thanks

@Stuart_Packham, I don’t have an example with using the $in , regex and golang driver. But, you can do something like this using the regex or operator:

queryWord := "^(Smith|John)"
query := bson.M{"name": bson.M{"$regex": queryWord, "$options": "i"}}

This will find 3 matching names from:

"name" : "Johnson" }
"name" : "John" }
"name" : "Smithy" }
"name" : "James" }

EDIT ADD: The following query filter returns the same result as the above query - this one uses the $in operator with regex:

rgx1 := primitive.Regex{Pattern: "^Smith", Options: "i"}
rgx2 := primitive.Regex{Pattern: "^John", Options: "i"}
query := bson.D{{"name", bson.D{{"$in", bson.A{rgx1, rgx2}}}}}