How to dynamically apply a conditional operator to a field using the official MongoDB-CSharp-Driver?

I am trying to create a query that finds all big, red things with a value greater than 3.

This query seems to be what I need:

{ "color" : "red", "size" : "large", "cost" : { "$gt" : 3.0 } }

But I can’t find an elegant way to create a cost condition using the official MongoDB CSharp driver. This is one hack that seems to create a request:

QueryConditionList gt = Query.GT("cost", BsonDouble.Create(3));

QueryDocument query = new QueryDocument();
query.Add("color", "red");
query.Add("size", "large");
query.Add(gt.ToBsonDocument().Elements);

List<BsonDocument> results = events.Find(query).ToList();

Another way to do this, which works, is as follows:

QueryDocument query = new QueryDocument();
query.Add("color", "red");
query.Add("size", "large");
query.Add("cost", new BsonDocument("$gt", BsonDouble.Create(3)));

List<BsonDocument> results = events.Find(query).ToList();

Is any of these approaches a good way to achieve this? Is there some more?

I need to use methods that allow me to dynamically build a query and add fields that will be involved in the query. I was hoping to find a way to add a condition through query.Add (), but I don't know if this is possible.

Any help is appreciated.

+5
2

, :

var query = Query.And(
              Query.EQ("color", "red"), 
              Query.EQ("size", "large"), 
              Query.GT("cost", 3)
            );

update , , .

- :

int i = 0;
var qc = QueryComplete[3];
qc[i++] = Query.EQ("color", "red");
qc[i++] = Query.EQ("size", "large");
qc[i++] = Query.GT("cost", 3);
var query = Query.And(qc);

, .

+6

, "QueryElement" BuildQuery, , :

    public class QueryElement
    {
        public enum eOperator
        {
            AND, OR, EQ, NE, GT, GTE, LT, LTE      //etc.
        };

        public eOperator Operator { get; set; }

        public string Field { get; set; }

        public BsonValue Value { get; set; }

        public List<QueryElement> Children { get; set; }

        public IMongoQuery BuildQuery()
        {
            int i = 0;
            var qc = new IMongoQuery[(Children!=null)?Children.Count:0];

            if (Children != null)
            {
                foreach (var child in Children)
                {
                    qc[i++] = child.BuildQuery();
                }
            }

            switch (Operator)
            {
                // multiple element operators

                case eOperator.AND:
                    return Query.And(qc);
                case eOperator.OR:
                    return Query.And(qc);

                // single element operators
                case eOperator.EQ:
                    return Query.EQ(Field, Value);
                case eOperator.NE:
                    return Query.NE(Field, Value);
                case eOperator.GT:
                    return Query.GT(Field, Value);
                case eOperator.GTE:
                    return Query.GTE(Field, Value);
                case eOperator.LT:
                    return Query.LT(Field, Value);
                case eOperator.LTE:
                    return Query.LTE(Field, Value);
            }
            return null;
        }
    }
+2

All Articles