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

Proposal: Improve a way of making filters and aggregation pipilines

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      For now to write filter (or aggregation) in golang we must construct bson.D object, and it is not handy for 2 reasons:
      1. This looks terrible
      2. You can not easily copy and paste query between mongo-compass/atlas, studio-3t or mongosh and your golang code.

      Example:

       

       

      f := bson.D{
          {Key: "id", Value: "abc"},
          {Key: "start", Value: bson.D{{Key: "$lte", Value: ts}}},
          {Key: "$or", Value: bson.A{
              bson.D{{Key: "end", Value: bson.D{{Key: "$exists", Value: false}}}},
              bson.D{{Key: "end", Value: nil}},
              bson.D{{Key: "end", Value: bson.D{{Key: "$gte", Value: ts}}}},
          }},
      }

       

      But what if we will use some helpers to work with text filters instead:

      f := mongo.MustCompileQuery(`{
              "id" : "$1",
              "start": { "$lte": "$2" },
              "$or": [
                  { "end": { "$exists": false } },
                  { "end": null },
                  { "end": { "$gte": "$2" } }
              ]}`,
          "$1", id,
          "$2", date,
      )
      

      Now filter looks more developer-friendly and you can copy and paste it between tools. We can cache compiled queries on client side so there will be almost no overhead.

      Example of implementation:
      https://gist.github.com/hummerd/0ef2991443e8ab064ef9bba168f97abb

            Assignee:
            Unassigned Unassigned
            Reporter:
            hummer.dk@gmail.com hummerd N/A
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: