简体   繁体   中英

MongoDB C# Driver QueryDocument greater than or less than

I am using C# driver for MongoDB and I have the QueryDocument working to query a string using Query.Add but I am trying to dynamically build this out (which works if I am doing Add for = a value. I want to do greater than or less than for a date check (dynamically). See my code below.

I want to do a query.Add event (or some other event) to add to a query for the datetime check for greater than or less than.

NOTE: I may be adding more values to search on other then the below so I know if I just search on LoanNumber I can ignore the date/time but I want to build out multiple search criteria.

For now I just need greater/less than using the C# driver, but pointers/help to where I can do other operations like regex or others would be helpful but not required (I can find these for generic MongoDB searches and other environments just not C# driver).

        var query = new QueryDocument();

        // check if there is anything in the loan number field and if so query that
        string LoanNumber = txtLoanNumber.Text.ToString();
        string StartDate = txtStatusChangeStartDate.Text.ToString();
        string EndDate = txtStatusChangeEndDate.Text.ToString();



        if (!string.IsNullOrEmpty(LoanNumber))
        {
            query.Add("LoanStatusAddedEvent.LoanNumber", LoanNumber);
        }

        if (!string.IsNullOrEmpty(StartDate))
        {
            query.Add("LoanStatusAddedEvent.StatusChangeDate", StartDate);
        }

        if (!string.IsNullOrEmpty(EndDate))
        {               
            query.Add(
                    -- saw this online to do it but it was a direct query and not adding to the Query.add
                    Query.LT("LoanStatusAddedEvent.StatusChangeDate", EndDate)
            );
        }

If you want to query multiple lines try to do like this:

  1. Collect your Query data:

    var queryList = new List();

    queryList.Add(Query.EQ("Attributes.AttributeName", attribute.AttributeName)); queryList.Add(Query.EQ("Attributes2.AttributeName2", attribute2.AttributeName2));

  2. Build your query:

    var query = new QueryBuilder(); query.And(queryList);

After that you can use your query :)

I hope i can help with your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM