MongoDB C # driver query

Source: Internet
Author: User

Inosqlprovider Provider = nosqlmanager.create ("cloudtable");
Imongocollection<formmongodbmodel> collection = provider. Getcollection<formmongodbmodel, imongocollection<formmongodbmodel>> (Ctid);

#region Query Condition Stitching
Switch (listquerymodel.querytype)
{
Case "equal"://Equals
if (Listquerymodel.fieldtype = = "Datetime")
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.eq ("field." + Listquerymodel.field + ".") + Queryconditiontype, Convert.todatetime (Listquerymodel.value)));
}
else if (Listquerymodel.fieldtype = = "Number" | | listquerymodel.fieldtype = = "Money" | | listquerymodel.fieldtype = = "Formu La ")
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.eq ("field." + Listquerymodel.field + ".") + Queryconditiontype, convert.todouble ((Listquerymodel.value)));
}
Else
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.eq ("field." + Listquerymodel.field + ".") + Queryconditiontype, listquerymodel.value));
}
Break
Case "notequal"://Not equal to
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.ne ("field." + Listquerymodel.field + ".") + Queryconditiontype, listquerymodel.value));
Break
Case ' like '://contains
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.regex ("field." + Listquerymodel.field + ".") + Queryconditiontype, listquerymodel.value));
Break
Case "Orlike":
string[] orlike_values = listquerymodel.value.Split (' | ');
list<filterdefinition<formmongodbmodel>> orlikelist = new List<filterdefinition<formmongodbmodel >> ();
for (int i = 0; i < orlike_values. Length; i++)
{
Orlikelist.add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.regex ("field." + Listquerymodel.field + ".") + Queryconditiontype, orlike_values[i]);
}
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.or (orlikelist));
Break
Case "Andlike":
string[] andlike_values = listquerymodel.value.Split (' | ');
list<filterdefinition<formmongodbmodel>> andlike_value = new list<filterdefinition< Formmongodbmodel>> ();
for (int i = 0; i < andlike_values. Length; i++)
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.regex ("field." + Listquerymodel.field + ".") + Queryconditiontype, andlike_values[i]);
}
Break
Case "Notlike":
string[] notlike_values = listquerymodel.value.Split (' | ');
list<filterdefinition<formmongodbmodel>> notlikelist = new list<filterdefinition< Formmongodbmodel>> ();
for (int i = 0; i < notlike_values. Length; i++)
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.not (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.regex ("field." + Listquerymodel.field + ".") + Queryconditiontype, notlike_values[i]));
}
Break
Case "NULL":
filterdefinition<formmongodbmodel> filter = new Bsondocument ("field." + Listquerymodel.field + "." + Queryconditiontype, bsonnull.value);
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.and (filter));
Break
Case "Notnull":
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.ne ("field." + Listquerymodel.field + ".") + Queryconditiontype, bsonnull.value));
Break
Case "or":
string[] or_values = listquerymodel.value.Split (' | ');
list<filterdefinition<formmongodbmodel>> orlist = new list<filterdefinition<formmongodbmodel> > ();
for (int i = 0; i < or_values. Length; i++)
{
Orlist.add (New Bsondocument ("field." + Listquerymodel.field + ".") + Queryconditiontype, or_values[i]);
}
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.or (orlist));
Break
Case "Greater":
if (Listquerymodel.fieldtype = = "Datetime")
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.gt ("field." + Listquerymodel.field + ".") + Queryconditiontype, Convert.todatetime (Listquerymodel.value)));
}
Else
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.gt ("field." + Listquerymodel.field + ".") + Queryconditiontype, listquerymodel.value));
}
Break
Case "Greaterequal":
if (Listquerymodel.fieldtype = = "Datetime")
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.gte ("field." + Listquerymodel.field + ".") + Queryconditiontype, Convert.todatetime (Listquerymodel.value)));
}
Else
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.gte ("field." + Listquerymodel.field + ".") + Queryconditiontype, listquerymodel.value));
}
Break
Case ' less ':
if (Listquerymodel.fieldtype = = "Datetime")
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. filter.lt ("field." + Listquerymodel.field + ".") + Queryconditiontype, Convert.todatetime (Listquerymodel.value)));
}
Else
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. filter.lt ("field." + Listquerymodel.field + ".") + Queryconditiontype, listquerymodel.value));
}
Break
Case "Lessequal":
if (Listquerymodel.fieldtype = = "Datetime")
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.lte ("field." + Listquerymodel.field + ".") + Queryconditiontype, Convert.todatetime (Listquerymodel.value)));
}
Else
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.lte ("field." + Listquerymodel.field + ".") + Queryconditiontype, listquerymodel.value));
}
Break
Case "between":
string[] between_values = listquerymodel.value.Split (' | ');
String between_value1 = Between_values[0];
String between_value2 = Between_values[1];
if (Listquerymodel.fieldtype = = "Datetime")
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.gt ("field." + Listquerymodel.field + ".") + Queryconditiontype, Convert.todatetime (between_value1)));
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. filter.lt ("field." + Listquerymodel.field + ".") + Queryconditiontype, Convert.todatetime (between_value2)));
}
Else
{
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. Filter.gt ("field." + Listquerymodel.field + ".") + Queryconditiontype, Convert.ToInt32 (between_value1)));
List. Add (BUILDERS&LT;FORMMONGODBMODEL&GT;. filter.lt ("field." + Listquerymodel.field + ".") + Queryconditiontype, Convert.ToInt32 (between_value2)));
}
Break

Default
Break
}

#endregion

result = collection. Find (BUILDERS<FORMMONGODBMODEL>. Filter.and (list). Skip (Pageskip). Limit (pagesize). ToList ();

MongoDB C # driver query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.