Hi. I’ve found a similar case that doesn’t seem to work.
// Try to find all projects in list with mongo-driver/mongo
var results []bson.M
cursor, err := ProjectCollection.Find(ctx,
bson.D{{
Key: "_id",
Value: bson.D{{Key: "$in", Value: obj.ProjectIDs}},
}},
)
cursor.All(ctx, &results)
fmt.Printf("id strings: %v\n", obj.ProjectIDs)
fmt.Printf("id types: %T\n", obj.ProjectIDs) // showing type of project ids list
fmt.Printf("results: %v\n", results)
id strings: ["idhexstring1", "idhexstring2", ...]
id types: []string
results []
//This works in Atlas in db.projects collection
{"_id": {"$in": [ObjectId("idhexstring1"), ObjectId("idhexstring2"), ...]} }
[{document1...},{document2...},...]
For the mongo-driver/mongo code, changing to bson.M does not change the result. Changing the project id list to be of type []primitive.ObjectId
yields an error that interface{} is string, not primitive.ObjectID
. Can someone confirm that the “$in” search does in fact work with db.Collection.Find() and possibly provide an example?
Edit: See @Divjot_Arora suggestion for the answer to my question