This time, the use of C#driver, MongoDB for a simple query.
We can use the static method in the query class to create a series of queries to achieve the purpose of the query.
Query is in the using MongoDB.Driver.Builders namespace.
1. Single condition query, you can refer to the following code:
var query = Query.eq ("ColName", "Value"); foreach (Bsondocument doc in collection. Find (query) { Debug.WriteLine (doc["colname"). ToString () + "\ t"); }
2. To make a combination of conditions, such as and, or or, refer to the code:
var query = query.or ( query.eq ("ColName1", "Value1"), query.eq ("ColName2", "Value2") ); foreach (Bsondocument doc in Dotnetcollection.find (query)) { Debug.WriteLine (doc["ColName1"]. ToString () + "\ T" + doc["ColName2"]. ToString ()); }
Using these QueryBuilder to create a query is a recommended method for MongoDB, simple and clear.
3. To return the specified object, you can refer to the code:
var query = Query<table1>. EQ (b = b.testcol1, "BBB"); foreach (table1 ele in dotnetcollection.findas<table1> (query)) { Debug.WriteLine (ele.testcol1 + "\ T") + ele.testcol2); }
where table1 is defined as:
public class table1 {public ObjectId _id {get; set;} public string TestCol1 {get; set;} public string TestCol2 {get; set;} }
It is important to note that _id must contain this attribute, ObjectId is MongoDB.Bson.ObjectId, otherwise it will produce "Element ' _id ' does not match any field or property in the data reading. Class "This error.