Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1201

How do we combine filters with the driver?

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: BSON, CRUD
    • Labels:
      None
    • Environment:
      Using MAC OS X, Goland

      Hey folks

       

      I am trying to combine some filters with no success, can someone help me out?

      ```

      // FindOneByID returns an error on fail, otherwise, it sets the {out} by the result.func FindOneByID(collectionName string, id *primitive.ObjectID, out interface{}, considerArchivedItems bool) error { ctx, _ := context.WithTimeout(context.Background(), 2*time.Second) var filter bson.M
      if considerArchivedItems { filter = bson.M

      {"_id": id}

      } else { filter = bson.M{"$and": bson.A{ bson.M

      {"_id": id}

      , bson.M{isArchived: bson.M{"$ne": false}}}} }
      return database.Collection(collectionName).FindOne(ctx, filter).Decode(out)}

      ```

      Now, I insert an object as archived and trying to get the document. The document is inserted as archived, so I want to consider archived items.
      However, when I pass in the `else`, document can be either archived or not (`false` or `true`), I still get it anyway. So it means that my $and doesn’t work..

      ```

      const ( databaseNameUnitTests           = "database_name_unit_tests" databaseCollectionNameUnitTests = "database_name_unit_tests")
      type Test struct { ID          *primitive.ObjectID `protobuf:"bytes,1,opt,name=id,proto3" json:"_id,omitempty" bson:"_id,omitempty"` IsArchived  bool                `protobuf:"varint,2,opt,name=IsArchived,proto3" json:"is_archived,omitempty" bson:"is_archived,omitempty"` Description string              `protobuf:"varint,3,opt,name=Description,proto3" json:"description,omitempty" bson:"description,omitempty"`}
      // func    var item Test    err := mongodb.FindOneByID(databaseCollectionNameUnitTests, insertedID, &item, false)

      ```

      The following query from Robo 3T : `db.getCollection('database_name_unit_tests').find({$and:[

      {"_id":ObjectId("5d2e8afeb0e106fbe0d5af21")}

      ,{"is_archived": {$ne:false}}]})`this works tho, where am I wrong?

            Assignee:
            divjot.arora@mongodb.com Divjot Arora (Inactive)
            Reporter:
            maxime.guittet@say-eyes.com Maxime Guittet
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: