3

I have 2 structs:

type Customer struct {
    MyID int    `bson:"myID"`
}
type Bag struct {
    Customer primitive.ObjectID `bson:"customer"`
}

But I dunno how to make filter to find document in Bag collection by Customer.MyID I tried something like this:

filter := bson.D{{"customer", bson.D{{"myID", 123456789}}}}
cur, err := collection.Find(context.TODO(), filter)

Also tried this:

filter := bson.D{{"customer.myID", bson.D{{"$eq", 123456789}}}}
cur, err := collection.Find(context.TODO(), filter)

And I always get nothing in my cur variable. Please help me how to use it right.

icza
  • 389,944
  • 63
  • 907
  • 827
stolichna9
  • 31
  • 1

1 Answers1

-1

The collection name must not appear in the filter, the collection on which you call Find() already represents (or should) the customer collection, so just use:

collection := client.Database("yourdatabase").Collection("customer")

filter := bson.M{
    "myID": 123456789,
}
cur, err := collection.Find(context.TODO(), filter)

Or with bson.D:

filter := bson.D{{"myID", 123456789}}
icza
  • 389,944
  • 63
  • 907
  • 827