Detailed tutorial of. Net core using mongodb

Time:2021-2-17

preface

Mongodb is written by C + +, which is an open source database system based on distributed and document storage.

Download address:

https://www.mongodb.com/download-center/community

Using in. Net core needs to introduce core package MongoDB.Driver

Add data:

//Connect with mongodb
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//Get the database, if not, create it automatically
IMongoDatabase db = client.GetDatabase("db1");
//Get set (table)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var data = new Student();
data.id = 1;
data.name  ="Jiangbei";
data.age = 22;
data.remarks  ="Not yet";
//Add a piece of data
student.InsertOne(data);

Check it in the graphical interface

Mongodb uses ID as the primary key by default, so it does not explicitly specify that ID is the primary key. There is no built-in “auto increment field” in mongdb. You can declare ID as objectid type, so that the field will be assigned automatically after insertion.

For example, create a class:

public class School
{
 public ObjectId id { get; set; }
 public string name { get; set; }
 public string address { get; set; }
}//The namespace ﹣ using needs to be introduced MongoDB.Bson ;

Of course, you can add or remove one more field after the school object. Mongodb is stored in JSON, so it can also be inserted directly in JSON format. Bsondocument object can be used as generic object.

//Connect with mongodb
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//Get the database, if not, create it automatically
IMongoDatabase db = client.GetDatabase("db1");
//Get set (table)
IMongoCollection<BsonDocument> document = db.GetCollection<BsonDocument>("School");
db.GetCollection<BsonDocument>("School");
Var JSON = "{ID: 1, name: 'XX school', address: 'No. XX, XXX road', remarks: 'none at the moment! '}";
BsonDocument bsons = BsonDocument.Parse(json);

There is a corresponding relationship between students and schools. We can add objects with nested relationship types


public class Student
{
 public int id { get; set; }
 public string name { get; set; }
 public int age { get; set; }
 public string remarks { get; set; }
 public School School { get; set; }
}
//Connect with mongodb
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//Get the database, if not, create it automatically
IMongoDatabase db = client.GetDatabase("db1");
//Get set (table)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
Student student1 = new Student();
student1.id = 2;
Student1.name = beiwanzhou;
student1.age = 22;
Student1.remarks = none at the moment;
School school = new School();
school.name = "xxxSchool";
school.address = "xxxAddress";
student1.School = school;
student.InsertOne(student1);

Data query:

//Connect with mongodb
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//Get the database, if not, create it automatically
IMongoDatabase db = client.GetDatabase("db1");
//Get set (table)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var data = Builders<Student>. Filter.Gt (M = > m.age, 21); // GT: greater than
var result = student.Find(data).ToList();

The nuget package we installed supports lamda expressions, and we can use conditional expressions to find data

//Connect with mongodb
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//Get the database, if not, create it automatically
IMongoDatabase db = client.GetDatabase("db1");
//Get set (table)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var data = Builders<Student>. Filter.Where (m => m.age > 21 && m. name.Contains (Jiang);
var result = student.Find(data).ToList();

Paging query:

//Connect with mongodb
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//Get the database, if not, create it automatically
IMongoDatabase db = client.GetDatabase("db1");
//Get set (table)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var filter = Builders<Student>.Filter.Where(m => m.age > 21);
FindOptions<Student, Student> findOpt = new FindOptions<Student, Student>();
findOpt.Limit = 2;
findOpt.Skip = 1;
findOpt.Sort = Builders<Student>.Sort.Ascending(m => m.age).Descending(m => m.name);
var result = (student.FindAsync(filter, findOpt).Result).ToList();

Data update:

//Connect with mongodb
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//Get the database, if not, create it automatically
IMongoDatabase db = client.GetDatabase("db1");
//Get set (table)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var filter = Builders<Student>.Filter.Where(m => m.age > 21);
var update = Builders<Student>. Update.Set (M = > m.name, "Pikachu");
//Update student set name = "Pikachu" where age > 21
student.UpdateMany(filter, update);

Data deletion:

//Connect with mongodb
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//Get the database, if not, create it automatically
IMongoDatabase db = client.GetDatabase("db1");
//Get set (table)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var filter = Builders<Student>.Filter.Where(m => m.age > 21);
//delete from Student where age>21
//student.DeleteMany(filter);
student.DeleteOne (filter); // delete only one

Mongodb Chinese website: https://www.mongodb.org.cn

summary

Here is the article about the complete steps of using mongodb with. Net core. For more information about using mongodb with. Net core, please search previous articles of developer or continue to browse the following articles. I hope you can support developer more in the future!