Article Directory
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)
- 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)
- It is a product that writes massive amounts of data quickly under high concurrency
- You can write filter conditions.
The properties of mongodb (the difference from relational databases)
- database database (same as mysql)
- collection collection (called table in mysql)
- docment document (called a row of data in mysql)
The advantages of mongodb (compared with relational data)
- We don’t need to design our data structure in advance
- 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:
Four. Use C# to realize the operation of mongodb
4.1 Download the third-party nuget package
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:
Reference catalog
[1] The official website of mongdo's C# operation