This article is a little note on the study of mongodb. It mainly introduces the simplest addition, deletion and modification operations. Beginners, looking at the API, if there is any mistake, I hope you can correct me: (using the official driver)
The addition operation is the easiest, just construct bsonDcument and insert it:
Method 1, direct construction:
MongoServer dbserver = new MongoClient(connectionStr).GetServer();
MongoDatabase db = dbserver.GetDatabase(dbName);
MongoCollection collection = db.GetCollection(collectionName);
dbserver.Connect();
BsonDocument doc = new BsonDocument();
doc[“Age”] = Int32.Parse(txt_Age.Text);
Doc[“Name”] = txt_Name.Text;
doc[“Num”] = txt_Num.Text;
doc[“Introduction”] = txt_Introduction.Text;
Collection.Insert(doc);
Method 2, through entity construction:
1 var student = new Student
2 {
3 Age = Int32.Parse(txt_Age.Text),
4 Name = txt_Name.Text,
5 Num = txt_Num.Text,
6 Introduction = txt_Introduction.Text
7 };
8
9 collection.Insert(student);
The key is to construct the deletion condition, and find the signature of the Remove method through the api: public virtual WriteConcernResult
Remove(IMongoQuery
query); I saw a lot of ways to write BsonDocument objects in Remove on the Internet, but I checked the source code and found that bsonDocument does not implement the IMongoQuery interface at all. The interface is implemented by a class called QueryDocument, and QueryDocument also inherits the BsonDocument object. And there are many ways to construct BsonDocument and QueryDocument, all kinds of convenience, simply write a few:
For example, construct the following condition, delete from table where Age>15
&Age<20;
The corresponding mongodb condition writing method: {Age:{$gt:15,$lt:20}}, the following is to construct this condition;
Method 1, directly through bsonDocument construction:
BsonDocument doc = new BsonDocument
{
{ “Age”,new BsonDocument{{“$gte”,10},{“$lte”,15}}}
};
Method 2, directly through QueryDocument construction: similar to 1
1 QueryDocument query = new QueryDocument
2 {
3 { “Age”,new QueryDocument{{“$gte”,10},{“$lte”,15}}}
4 };
Method 3, directly by deserializing the json string:
1 string json = “{ Age:{$gte:10,$lte:15}}”;
2 var queryJson = BsonSerializer. Deserialize(json, typeof(BsonDocument)) as BsonDocument;
Personally, I think this method is very good. If you are familiar with mongodb commands, this method is very suitable for constructing complex conditions.
Method 4: Through the Query class, Query is a static class that encapsulates various logical conditional methods. There are two ways: generic and generic:
1 var query1 = Query.GT(“Age”, 10);//greater than 10; greater than 10
2 var query2 = Query.LT(“Age”, 15);//less than 15; less than 15
3 var query = Query.And(query1, query2);
But even better is the generic way:
var query1 = Query.GTE(t => t.Age, 10);
var query2 = Query.LTE(t => t.Age, 15);
//var query = Query.And(Query.GTE(“Age”, 10), Query.LTE(“Age”, 15));
var query = Query.And(query1, query2);
Finally, execute the Remove method;
Data display is essential. The conditional filtering in the query operation has been mentioned in the deletion, so I won’t go into details. Here are two ways to write it first (ps: I don’t understand it now, so I can only record it in the form of notes)
Method 1: Through the FindAllAs method or the FindAs method
1 var query1 = Query.GTE(t => t.Age, 10);
2 var query2 = Query.LTE(t => t.Age, 15);
3 var query = Query.And(query1, query2);
4
5 var list = collection.FindAs(typeof(Student), query);
Way 2: via linq
1 var qList = (from c in collection. AsQueryable()
2 where c.Age > 10 && c.Age < 15
3 Select c).ToList();