Getting started with MongoDb (1)



1. The difference between mongodb and redis

mongoDb is a database written in C++ and based on distributed file storage. And redis is written based on the c language. mongDB is a product between relational databases and non-relational databases. It is the database with the most abundant functions among non-relational databases and most resembling relational databases. The data structure he supports is very loose, and the query statement is very powerful.



2. Under what circumstances should mongodb be used?

Requirement 1 :
If you use redis for massive data, it is easy to cause memory to run out. From the memory point of view, the only way is to add memory sticks.
Is there a kind of database that can store the data we frequently use in the memory, and store some infrequently used data in the hard disk. mongodb exists between relational and non-relational databases.

Requirement 2 :
I need to find the logs recorded in the current time period according to the time period. To write the code, first use the core code to find out all the data in redis. Then for, and then filter.

Advantages of mongodb (compared to redis)

  1. There is only one point to save memory and realize hot and cold data (hot data is stored in the memory, and cold data is stored in the hard disk)
  2. It is a product that writes massive amounts of data quickly under high concurrency
  3. You can write filter conditions.

The properties of mongodb (the difference from relational databases)

  1. database database (same as mysql)
  2. collection collection (called table in mysql)
  3. docment document (called a row of data in mysql)

The advantages of mongodb (compared with relational data)

  1. We don’t need to design our data structure in advance
  2. We want to write data into it, as long as it is in json format.



3. Simple usage of MongoDb

The basic commands executed are somewhat similar to SQL statements (just use mongodb's management software to perform operations).

db.test001.insert({
    
    id:1,age:12,address:"abcd"})

db.test001.find({
    
    })
   .projection({
    
    })
   .sort({
    
    _id:-1})
   .limit(100)

Directly mongodb, you can directly mongodb 管理软件operate to. The grammar is rich and flexible

for(var i=1;i<11;i++){
    
    
    db.test001.insert([{
    
    id:i,name:"aass"+i}])
}

db.test001.find({
    
    })
   .projection({
    
    })
   .sort({
    
    _id:-1})
   .limit(100)

The results after execution are as follows:
Insert picture description here



Four. Use C# to realize the operation of mongodb

4.1 Download the third-party nuget package

Insert picture description here

4.2 Use code to implement operations on the mongdb database

The following is the operation using Alibaba Cloud's mysql database

public  class JsonOperation
    {
    
    
        public static void Show() {
    
    
            // mongdb的数据库连接
            var client = new MongoClient("mongodb://47.92.100.188:5008"); 
            var databse = client.GetDatabase("test01"); // 要找哪个数据库
            var document = BsonDocument.Parse("{a:1,b:[{c:1}],D:'ff'}");// 要写入的内容
            databse.GetCollection<BsonDocument>("test001").InsertOne(document);// 写到表里;
        }
    }

Database structure:
Insert picture description here




Reference catalog

[1] The official website of mongdo's C# operation

[2] https://www.bilibili.com/video/BV1Pi4y1x7p2?p=11&t=848

[3] https://www.runoob.com/mongodb/mongodb-tutorial.html

Guess you like

Origin blog.csdn.net/zhaozhao236/article/details/114440555