【NetCore】MongoDB安装与基本使用
MongoDB
安装
官网下载安装
https://www.mongodb.com/try/download/community
使用Docker安装
# 拉取镜像
docker pull mongo
# 运行
docker run -itd --name mongo -p 27017:27017 mongo --auth
# 进入容器
docker exec -it mongo mongo admin
# 创建用户
db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
# 尝试连接
db.auth('admin', '123456')
.Net 中客户端使用
安装 NuGet包
Install-Package MongoDB.Driver -Version 2.15.0
基本使用
using MongoDB.Driver;
using Newtonsoft.Json;
// 创建客户端
var client = new MongoClient("mongodb://admin:123456@localhost:27017/?authSource=admin");
using (var cursor = await client.ListDatabasesAsync())
{
await cursor.ForEachAsync(document => Console.WriteLine(document.ToString()));
var db = client.GetDatabase("perry_test");
var collection = db.GetCollection<Product>(nameof(Product));
// 添加数据到 mongo
await collection.InsertOneAsync(new Product
{
Id = 1,
Name = "XX产品",
Desc = "XXXXXXXXXXXXXX"
});
var query = collection.AsQueryable<Product>();
var res = query.Where(t => t.Id == 1).FirstOrDefault();
// 输出结果
Console.WriteLine(JsonConvert.SerializeObject(res));
}
/// <summary>
/// 实体-测试用
/// </summary>
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Desc { get; set; }
}
尝试使用 Linq 连接查询
代码
using MongoDB.Driver;
using Newtonsoft.Json;
// 创建客户端
var client = new MongoClient("mongodb://admin:Aa123456.@admin.wosperry.com:18100/?authSource=admin");
using (var cursor = await client.ListDatabasesAsync())
{
await cursor.ForEachAsync(document => Console.WriteLine(document.ToString()));
var db = client.GetDatabase("perry_test");
// 准备两个集合
var products = db.GetCollection<Product>(nameof(Product));
var productTypes = db.GetCollection<ProductType>(nameof(ProductType));
// 准备假数据
await productTypes.InsertManyAsync(new List<ProductType> {
new ProductType {
Id = 1,
Name = "电子产品"
},
new ProductType {
Id = 2,
Name = "毛绒玩具"
}
});
// 准备假数据
await products.InsertManyAsync(new List<Product>
{
new Product {
Id = 1,
TypeId = 1,
Name = "XX产品1",
Desc = "XXXXXXXXXXXXXX1"
},
new Product {
Id = 2,
TypeId = 1,
Name = "XX产品2",
Desc = "XXXXXXXXXXXXXX2"
},
new Product {
Id = 3,
TypeId = 2,
Name = "XX产品3",
Desc = "XXXXXXXXXXXXXX3"
}
});
// 尝试 Linq 查询
var query = from p in products.AsQueryable()
join t in productTypes.AsQueryable() on p.TypeId equals t.Id
select new
{
Id = p.Id,
TypeId = t.Id,
TypeName = t.Name,
Name = p.Name,
Desc = p.Desc,
};
var res = query.ToList();
// 显示结果
Console.WriteLine(JsonConvert.SerializeObject(res, Formatting.Indented));
}
/// <summary>
/// 测试
/// </summary>
public class Product
{
public int Id { get; set; }
public int TypeId { get; set; }
public string Name { get; set; }
public string Desc { get; set; }
}
public class ProductType
{
public int Id { get; set; }
public string Name { get; set; }
}
执行结果
[
{
"Id": 1,
"TypeId": 1,
"TypeName": "电子产品",
"Name": "XX产品1",
"Desc": "XXXXXXXXXXXXXX1"
},
{
"Id": 2,
"TypeId": 1,
"TypeName": "电子产品",
"Name": "XX产品2",
"Desc": "XXXXXXXXXXXXXX2"
},
{
"Id": 3,
"TypeId": 2,
"TypeName": "毛绒玩具",
"Name": "XX产品3",
"Desc": "XXXXXXXXXXXXXX3"
}
]
封装仓储
TODO
总结
TODO
博客园:https://www.cnblogs.com/wosperry/p/net6_mongo.html