【MongoDB】C# .Net MongoDB常用语法
1.1.驱动安装
1.2. C#连接MongoDB
//获取MongoDB连接客户端
MongoClient client = new MongoClient("mongodb://root:root@localhost:27017");
//获取数据库
var database = client.GetDatabase("mongtestdb");
//获取集合
var collection = database.GetCollection<Organizations>("Organizations"); 在获取集合时,有两种方式
1.指定集合类型,入上面的方式,指定获取集合时使用指定的类Organizations,优势是可以使用lamda表达式获取属性,查询时不需要转换类型
2.使用BsonDocument获取集合,查询结果若使用类接收,需要转换类型:C#中使用MongoDB聚合管道时必须使用BsonDocument的方式获取集合(目前是)
var collection = database.GetCollection<BsonDocument>("Organizations");
2.简单文档结构
//组织表结构
//Organizations
{
"_id": ObjectId("60f7961699515da94c20296c"),
"CreationTime": ISODate("2015-12-11T10:35:25.000Z"),
"CreatorUserId": NumberLong("date
1"),
"LastModificationTime": ISODate("2021-07-22T18:54:46.803Z"),
"LastModifierUserId": NumberLong("11949267"),
"IsDeleted": false,
"DeletionTime": null,
"DeleterUserId": null,
"CreatorUserName": null,
"LastModifierUserName": null,
"DeleterUserName": null,
"OrgId": NumberInt("2"), //组织Id
"Name": "某某集团", //组织名称
"ShortName": "某某集团", //组织简称
"Code": "HQW", //组织编码
"Type": NumberInt("100"), //组织类型:公司、部门
"HeadUserId": NumberLong("11944818"),
"Sort": NumberInt("1"),
"ParentId": NumberInt("0"), //上级组织Id
"IsActive": true, //是否启用
"OrganizationLevel": NumberInt("1"),
"Leaders":[{
"UserId":"abc",
"IsMain":true
}]
}
2.1.查询
//1.查询全部
db.Organizations.find({}) //2.通过Id查询
db.Organizations.find({"_id":ObjectId("60cac0979b3c533a5d1a06e1")}) //3.模糊查询
db.Organizations.find({"ShortName":/互联科技/}) //全模糊
db.Organizations.find({"ShortName":/^互联科技/}) //以“联科技公司”为前缀
db.Organizations.find({"ShortName":/联科技公司$/}) //以“联科技公司”为后缀 //4.in查询
db.Organizations.find({"ShortName":{$in:["电影视效中心","电影特效制作中心"]}}) //5.and查询
db.Organizations.find({"ShortName":"互联科技","Name":"互联科技科技有限公司"}) //6.or查询
db.Organizations.find({$or:[{"ShortName":"电影视效中心"},{"ShortName":"电影特效制作中心"}]}) //7.混合查询
//类似sql: Type=100 and OrgId>=2 and (Name like '%互联%' or ShortName='智能公司') and Code in ('SZN','SHL')
db.Organizations.find({
"Type": NumberInt(100),
"OrgId": {
$gte: 2
},
$or: [{
"Name": /互联/
}, {
"ShortName": "智能公司"
}],
"Code": {
$in: ["SZN", "SHL"]
}
})
C#语法
var filterBuilder = Builders<Organizations>.Filter; //创建过滤条件构建器
{
//1.查询全部
var filter = filterBuilder.Empty; //查询条件为空
var res = await collection.Find(filter).ToListAsync();
}
{
//2.通过Id查询
var filter = filterBuilder.Eq(a => a.Id, ObjectId.Parse("60cac0979b3c533a5d1a06e1"));
var res = await collection.Find(filter).ToListAsync();
}
{
//3.模糊查询
var filter = filterBuilder.Regex(a => a.ShortName,"/互联科技/");
var filter2 = filterBuilder.Regex(a => a.ShortName,"/^互联科技/");
var filter3 = filterBuilder.Regex(a => a.ShortName,"/联科技公司$/");
var res = await collection.Find(filter).ToListAsync();
}
{
//4.in查询
var filter = filterBuilder.In(a => ShortName, new[] { "电影视效中心","电影特效制作中心" });
var res = await collection.Find(filter).ToListAsync();
}
{
//5.and查询
var filter = filterBuilder.And(
filterBuilder.Eq(a => a.ShortName,"互联科技"),
filterBuilder.Eq(a => a.Name,"互联科技科技有限公司"),
);
var res = await collection.Find(filter).ToListAsync();
}
{
//6.or查询
var filter = filterBuilder.Or(
filterBuilder.Eq(a => a.ShortName,"电影视效中心"),
filterBuilder.Eq(a => a.ShortName,"电影特效制作中心"),
);
var res = await collection.Find(filter).ToListAsync();
}
{
//7.混合查询
//类似sql: Type=100 and OrgId>=2 and (Name like '%互联%' or ShortName='智能公司') and Code in ('SZN','SHL')
var filter = filterBuilder.And(
filterBuilder.Eq(a => a.Type, 100),
filterBuilder.Gte(a => a.OrgId, 2),
filterBuilder.Or(
filterBuilder.Regex(a => a.Name,"/互联/"),
filterBuilder.Eq(a => a.ShortName, "智能公司")
),
filterBuilder.In(a => a.Code, new[] { "SZN","SHL" })
);
var res = await collection.Find(filter).ToListAsync();
}
2.2.新增
//1.单条新增
db.Organizations.insertOne( {
CreationTime: ISODate("2015-12-11T10:35:25.000Z"),
CreatorUserId: NumberLong("1"),
LastModificationTime: ISODate("2021-07-22T18:54:46.803Z"),
LastModifierUserId: NumberLong("11949267"),
IsDeleted: false,
DeletionTime: null,
DeleterUserId: null,
CreatorUserName: null,
LastModifierUserName: null,
DeleterUserName: null,
OrgId: NumberInt("2"),
Name: "华强方特文化科技集团股份有限公司",
ShortName: "华强方特集团",
Code: "HQW",
Type: NumberInt("100"),
HeadUserId: NumberLong("11944818"),
Sort: NumberInt("1"),
ParentId: NumberInt("0"),
IsActive: true,
OrganizationLevel: NumberInt("1")
} ); //2.多条新增
db.Organizations.insertMany( [{
CreationTime: ISODate("2015-12-11T10:35:25.000Z"),
CreatorUserId: NumberLong("1"),
LastModificationTime: ISODate("2021-07-22T18:54:46.803Z"),
LastModifierUserId: NumberLong("11949267"),
IsDeleted: false,
DeletionTime: null,
DeleterUserId: null,
CreatorUserName: null,
LastModifierUserName: null,
DeleterUserName: null,
OrgId: NumberInt("2"),
Name: "华强方特文化科技集团股份有限公司",
ShortName: "华强方特集团",
Code: "HQW",
Type: NumberInt("100"),
HeadUserId: NumberLong("11944818"),
Sort: NumberInt("1"),
ParentId: NumberInt("0"),
IsActive: true,
OrganizationLevel: NumberInt("1")
}] );
//1.单条新增
await collection.InsertOneAsync(new Organizations
{
OrgId = 2,
Name = "华强方特文化科技集团股份有限公司",
IsActive = true,
CreationTime = DateTime.Now,
... ...
}); //2.多条新增
await collection.InsertManyAsync(new List<Organizations>());
2.3.更新
//1.局部更新
db.Organizations.updateOne(
{ Code: "HQW" }, //查询条件
{
$set: { "ShortName": "华强方特", IsActive: false }, //待更新字段
$push: { //在数组中插入元素
"Leaders": {
"UserId": "ggggg",
"IsMain": true
}
},
$currentDate: { LastModificationTime: true } //更新字段LastModificationTime为当前时间,若该字段不存在,则创建
}
) //2.多条更新
db.Organizations.updateMany(
{ LastModificationTime: {$gte:ISODate("2021-07-22T18:54:46.803Z")} }, //查询条件
{
$set: { "ShortName": "华强方特", IsActive: false }, //待更新字段
$currentDate: { LastModificationTime: true } //更新字段LastModificationTime为当前时间,若该字段不存在,则创建
}
) //3.替换更新
db.Organizations.replaceOne(
{"Code": "HQW"}, //查询条件
{ //带替换的文档
"_id": ObjectId("60f7961699515da94c20296c"),
CreationTime: ISODate("2015-12-11T10:35:25.000Z"),
CreatorUserId: NumberLong("1"),
LastModificationTime: ISODate("2021-07-22T18:54:46.803Z"),
LastModifierUserId: NumberLong("11949267"),
IsDeleted: false,
DeletionTime: null,
DeleterUserId: null,
CreatorUserName: null,
LastModifierUserName: null,
DeleterUserName: null,
OrgId: NumberInt("2"),
Name: "华强方特文化科技集团股份有限公司",
ShortName: "华强方特集团",
Code: "HQW",
Type: NumberInt("100"),
HeadUserId: NumberLong("11944818"),
Sort: NumberInt("1"),
ParentId: NumberInt("0"),
IsActive: true,
OrganizationLevel: Num
berInt("1")
}
)
//1.局部更新
var filter = Builders<Organizations>.Filter.Eq(a => a.Id, ObjectId.Parse("60cac0979b3c533a5d1a06e1"));
var updateBuilder = Builders<Organizations>.Update;
var updates = new List<UpdateDefinition<Organizations>>
{
updateBuilder.Set(a=> a.ShortName,"华强方特"),
updateBuilder.Set(a=> a.IsActive,true),
updateBuilder.Push(a=> a.Leaders,new { UserId = "abc",IsMain = true}),
updateBuilder.CurrentDate(a=>a.LastModificationTime)
};
var res = await collection.UpdateOneAsync(filter, updateBuilder.Combine(updates)); //2.多条更新
var filter = Builders<Organizaionts>.Filter.Gte(a => a.LastModificationTime, DateTime.Now.AddDays(1));
var updateBuilder = Builders<Organizaionts>.Update;
var updates = new List<UpdateDefinition<Organizaionts>>
{
updateBuilder.Set(a=> a.ShortName,"华强方特"),
updateBuilder.Set(a=> a.IsActive,true),
updateBuilder.CurrentDate(a=>a.LastModificationTime)
};
var res = await collection.UpdateManyAsync(filter, updateBuilder.Combine(updates)); //3.替换更新
var replaceModel = new Organizaionts(){...};
var replaceRes = await collection.ReplaceOneAsync(Builders<Organizations>.Filter.Eq(a => Code, "HQW"), replaceModel);
3.嵌套文档结构
//EmployeeFieldInfos
{
"_id": ObjectId("609cfe663242fa662691b34a"),
"CreationTime": ISODate("2021-05-13T18:24:37.579Z"),
"CreatorUserId": null,
"LastModificationTime": ISODate("2021-05-13T18:24:37.583Z"),
"LastModifierUserId": null,
"IsDeleted": false,
"DeletionTime": null,
"DeleterUserId": null,
"CreatorUserName": null,
"LastModifierUserName": null,
"DeleterUserName": null,
"Categories": [
{
"Code": "PersonalInfo",
"Name": "个人信息设置",
"Sort": NumberInt("1"),
"Groups": [
{
"Code": "BasicInfoGroup",
"Name": "基本信息",
"Sort": NumberInt("1"),
"EmployeeFields": [
{
"Code": "470a584e78f24c9da8c69e3cf2dabf48",
"Name": "爱好",
"IsSystemDefault": false,
"FieldDataType": NumberInt("0"),
"StringLength": NumberInt("125"),
"Prompt": "最大支持125个字符",
"IsRequried": false,
"Sort": NumberInt("2")
},
{
"Code": "d7b5ad358cc64d0e8f9379eb1c9b23cf",
"Name": "姓名",
"IsSystemDefault": false,
"FieldDataType": NumberInt("0"),
"StringLength": NumberInt("125"),
"Prompt": "最大支持125个字符",
"IsRequried": false,
"Sort": NumberInt("1")
}
]
},
{
"Code": "CommunicationInfoGroup",
"Name": "通讯信息",
"Sort": NumberInt("2"),
"EmployeeFields": [ ]
}
]
},
{
"Code": "PostInfo",
"Name": "岗位信息设置",
"Sort": NumberInt("2"),
"Groups": [
{
"Code": "PostInfoGroup",
"Name": "岗位信息",
"Sort": NumberInt("1"),
"EmployeeFields": [ ]
},
{
"Code": "ContractInfoGroup",
"Name": "合同信息",
"Sort": NumberInt("2"),
"EmployeeFields": [ ]
}
]
}
]
}
3.1.拆分查询
db.EmployeeFieldInfos.aggregate([{
$unwind: '$Categories' //拆分的子文档
}, {
$unwind: '$Categories.Groups' //拆分的子文档
}, {
$unwind: '$Categories.Groups.EmployeeFields' //拆分的子文档
}, {
$match: {
'Categories.Code': {
$in: ['PersonalInfo']
},
'Categories.Groups.Code': {
$in: ['BasicInfoGroup']
},
'Categories.Groups.EmployeeFields.IsDeleted': false
}
}, {
$addFields: { //临时变量
'Code': '$Categories.Groups.EmployeeFields.Code',
'Name': '$Categories.Groups.EmployeeFields.Name',
'IsSystemDefault': '$Categories.Groups.EmployeeFields.IsSystemDefault',
'FieldDataType': '$Categories.Groups.EmployeeFields.FieldDataType',
'OptionsDefinitionCode': '$Categories.Groups.EmployeeFields.OptionsDefinitionCode',
'StringLength': '$Categories.Groups.EmployeeFields.StringLength',
'Prompt': '$Categories.Groups.EmployeeFields.Prompt',
'IsRequired': '$Categories.Groups.EmployeeFields.IsRequired',
'Sort': '$Categories.Groups.EmployeeFields.Sort',
'DefaultValue': '$Categories.Groups.EmployeeFields.DefaultValue',
'IsActive': '$Categories.Groups.EmployeeFields.IsActive',
'IsDeleted': '$Categories.Groups.EmployeeFields.IsDeleted',
'CategoryCode': '$Categories.Code',
'CategoryName': '$Categories.Name',
'CategorySort': '$Categories.Sort',
'GroupCode': '$Categories.Groups.Code',
'GroupName': '$Categories.Groups.Name',
'GroupSort': '$Categories.Groups.Sort',
'GroupSystemDefault': '$Categories.Groups.IsSystemDefault',
'GroupMultiple': '$Categories.Groups.IsMultiple'
}
}, {
$project: { //表示需要显示或隐藏的属性列表,0表示隐藏,1表示显示。此处可使用上面定义的临时变量
'_id': 0, //_id属性属于文档顶层元素,不需要使用临时变量
'Code': 1, //当前Code属于嵌套子文档中的元素,需要使用临时变量
'Name': 1,
'IsSystemDefault': 1,
'FieldDataType': 1,
'OptionsDefinitionCode': 1,
'StringLength': 1,
'Prompt': 1,
'IsRequired': 1,
'Sort': 1,
'DefaultValue': 1,
'IsActive': 1,
'IsDeleted': 1,
'CategoryCode': 1,
'CategoryName': 1,
'CategorySort': 1,
'GroupCode': 1,
'GroupName': 1,
'GroupSort': 1,
'GroupSystemDefault': 1,
'GroupMultiple': 1
}
}]); //结果实例:
// 1
{
"IsDeleted": false,
"Code": "470a584e78f24c9da8c69e3cf2dabf48",
"Name": "爱好",
"IsSystemDefault": false,
"FieldDataType": NumberInt("0"),
"StringLength": NumberInt("125"),
"Prompt": "最大支持125个字符",
"Sort": NumberInt("2"),
"CategoryCode": "PersonalInfo",
"CategoryName": "个人信息设置",
"CategorySort": NumberInt("1"),
"GroupCode": "BasicInfoGroup",
"GroupName": "基本信息",
"GroupSort": NumberInt("1"),
... ...
} // 2
{
"IsDeleted": false,
"Code": "d7b5ad358cc64d0e8f9379eb1c9b23cf",
"Name": "姓名",
"IsSystemDefault": false,
"FieldDataType": NumberInt("0"),
"StringLength": NumberInt("125"),
"Prompt": "最大支持125个字符",
"Sort": NumberInt("1"),
"CategoryCode": "PersonalInfo",
"CategoryName": "个人信息设置",
"CategorySort": NumberInt("1"),
"GroupCode": "BasicInfoGroup",
"GroupName": "基本信息",
"GroupSort": NumberInt("1"),
... ...
}
var collection = BsonDocumentCollection;
var pipelineJsons = new List<string> {
"{$unwind:'$Categories'}",
"{$unwind:'$Categories.Groups'}",
"{$unwind:'$Categories.Groups.EmployeeFields'}",
"{$match:{'Categories.Code':{$in:['PersonalInfo']},'Categories.Groups.Code':{$in:['BasicInfoGroup']},'Categories.Groups.EmployeeFields.IsDeleted':false}}",
"{$addFields:{'Code':'$Categories.Groups.EmployeeFields.Code','Name':'$Categories.Groups.EmployeeFields.Name','IsSystemDefault':'$Categories.Groups.EmployeeFields.IsSystemDefault','FieldDataType':'$Categories.Groups.EmployeeFields.FieldDataType','ExtensionValue':'$Categories.Groups.EmployeeFields.ExtensionValue','IsMultipleChoice':'$Categories.Groups.EmployeeFields.IsMultipleChoice','OptionsDefinitionCode':'$Categories.Groups.EmployeeFields.OptionsDefinitionCode','StringLength':'$Categories.Groups.EmployeeFields.StringLength','Prompt':'$Categories.Groups.EmployeeFields.Prompt','IsRequired':'$Categories.Groups.EmployeeFields.IsRequired','ReadOnly':'$Categories.Groups.EmployeeFields.ReadOnly','Sort':'$Categories.Groups.EmployeeFields.Sort','DefaultValue':'$Categories.Groups.EmployeeFields.DefaultValue','IsActive':'$Categories.Groups.EmployeeFields.IsActive','IsEdit':'$Categories.Groups.EmployeeFields.IsEdit','IsHide':'$Categories.Groups.EmployeeFields.IsHide','IsDeleted':'$Categories.Groups.EmployeeFields.IsDeleted','CategoryCode':'$Categories.Code','CategoryName':'$Categories.Name','CategorySort':'$Categories.Sort','GroupCode':'$Categories.Groups.Code','GroupName':'$Categories.Groups.Name','GroupSort':'$Categories.Groups.Sort','GroupSystemDefault':'$Categories.Groups.IsSystemDefault','GroupMultiple':'$Categories.Groups.IsMultiple','GroupReadOnly':'$Categories.Groups.ReadOnly'}}",
"{$project:{'_id':0,'Code':1,'Name':1,'IsSystemDefault':1,'FieldDataType':1,'ExtensionValue':1,'IsMultipleChoice':1,'OptionsDefinitionCode':1,'StringLength':1,'Prompt':1,'IsRequired':1,'ReadOnly':1,'Sort':1,'DefaultValue':1,'IsActive':1,'IsEdit':1,'IsHide':1,'IsDeleted':1,'CategoryCode':1,'CategoryName':1,'CategorySort':1,'GroupCode':1,'GroupName':1,'GroupSort':1,'GroupSystemDefault':1,'GroupMultiple':1,'GroupReadOnly':1}}"
};
var stages = pipelineJsons.Select(a => new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(a));
var pipeline = new PipelineStagePipelineDefinition<BsonDocument, BsonDocument>(stages);
var res = await collection.Aggregate(pipeline).ToListAsync();
if (res == null)
{
return new List<EmployeeFieldDto>();
}
var dotNetObjList = res.ConvertAll(BsonTypeMapper.MapToDotNetValue);
var json = JsonConvert.SerializeObject(dotNetObjList);
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<EmployeeFieldDto>>(json);
3.2.子文档新增
db.EmployeeFieldInfos.updateOne({}, {
$push: {
"Categories.$[i].Groups.$[j].EmployeeFields": {
"IsDeleted": false,
"Code": "d7b5ad358cc64d0e8f9379eb1c9b23cf",
"Name": "姓名",
"IsSystemDefault": false,
"FieldDataType": NumberInt("0"),
"StringLength": NumberInt("125"),
"Prompt": "最大支持125个字符",
"Sort": NumberInt("1"),
"CategoryCode": "PersonalInfo",
"CategoryName": "个人信息设置",
"CategorySort": NumberInt("1"),
"GroupCode": "BasicInfoGroup",
"GroupName": "基本信息",
"GroupSort": NumberInt("1")
}
}
}, {
arrayFilters: [{
"i.Code": "PersonalInfo"
}, {
"j.Code": "BasicInfoGroup"
}]
})
var collection = Collection;
var update = Builders<EmployeeFieldInfo>.Update.Push("Categories.$[i].Groups.$[j].EmployeeFields", new EmployeeFieldInfo());
var updateOption = new UpdateOptions
{
ArrayFilters = new List<ArrayFilterDefinition<EmployeeFieldInfo>> {
new BsonDocument("i.Code", categoryCode),
new BsonDocument("j.Code", groupCode)
}
};
var res = await collection.UpdateOneAsync(Builders<EmployeeFieldInfo>.Filter.Empty, update, updateOption);
if (res.MatchedCount == 0)
{
throw new ResultException(Result.FromError("新增失败"));
}
return res;
3.3.子文档更新
//1.修改部分属性
db.EmployeeFieldInfos.updateOne({}, {
$set: {
"Categories.$[i].Groups.$[j].EmployeeFields.$[k].Name": "姓名"
}
}, {
arrayFilters: [{
"i.Code": "PersonalInfo"
}, {
"j.Code": "BasicInfoGroup"
}, {
//对索引k的多个条件,需要包含在同一个json对象内,否则会报错:Found multiple array filters with the same top-level field name k
"k.Code": "Name",
"k.IsDeleted": true
}]
}) //2.修改多个属性
db.EmployeeFieldInfos.updateOne({}, {
$set: {
"Categories.$[i].Groups.$[j].EmployeeFields.$[k].Name": "姓名",
"Categories.$[i].Groups.$[j].EmployeeFields.$[k].IsActive": true
}
}, {
//嵌套文档过滤条件
arrayFilters: [{
"i.Code": "PersonalInfo"
}, {
"j.Code": "BasicInfoGroup"
}, {
//对索引k的多个条件,需要包含在同一个json对象内,否则会报错:Found multiple array filters with the same top-level field name k
"k.Code": "Name",
"k.IsDeleted": true
}]
}) //3.修改整个嵌套文档
db.EmployeeFieldInfos.update({}, {
$set: {
"Categories.$[i].Groups.$[j].EmployeeFields.$[k]": {
"IsDeleted": false,
"Code": "d7b5ad358cc64d0e8f9379eb1c9b23cf",
"Name": "姓名",
"IsSystemDefault": false,
"FieldDataType": NumberInt("0"),
"StringLength": NumberInt("125"),
"Prompt": "最大支持125个字符",
"Sort": NumberInt("1"),
"CategoryCode": "PersonalInfo",
"CategoryName": "个人信息设置",
"CategorySort": NumberInt("1"),
"GroupCode": "BasicInfoGroup",
"GroupName": "基本信息",
"GroupSort": NumberInt("1")
}
}
}, {
arrayFilters: [{
"i.Code": "PersonalInfo"
}, {
"j.Code": "BasicInfoGroup"
}, {
"k.Code": "Name",
"k.IsDeleted": true
}]
})
//1.修改一个属性
var collection = Collection;
var update = Builders<EmployeeFieldInfo>.Update.Set("Categories.$[i].Groups.$[j].EmployeeFields.$[k].Name", "姓名");
var updateOption = new UpdateOptions
{
ArrayFilters = new List<ArrayFilterDefinition<EmployeeFieldInfo>> {
new BsonDocument("i.Code", categoryCode),
new BsonDocument("j.Code", groupCode),
new BsonDocument(){
{ "k.Code", field.Code},
{ "k.IsDeleted",false}
}
}
};
var res = await collection.UpdateOneAsync(Builders<EmployeeFieldInfo>.Filter.Empty, update, updateOption);
if (res.MatchedCount == 0)
{
throw new ResultException(Result.FromError("更新失败"));
}
return res; //2.修改多个属性
var collection = Collection;
var updateBuilder = Builders<Employee>.Update;
var updates = updateBuilder.Combine(new List<UpdateDefinition>{
Builders<EmployeeFieldInfo>.Update.Set("Categories.$[i].Groups.$[j].EmployeeFields.$[k].Name", "姓名"),
Builders<EmployeeFieldInfo>.Update.Set("Categories.$[i].Groups.$[j].EmployeeFields.$[k].IsActive", true)
});
var updateOption = new UpdateOptions
{
ArrayFilters = new List<ArrayFilterDefinition<EmployeeFieldInfo>> {
new BsonDocument("i.Code", categoryCode),
new BsonDocument("j.Code", groupCode),
new BsonDocument(){
{ "k.Code", field.Code},
{ "k.IsDeleted",false}
}
}
};
var res = await collection.UpdateOneAsync(Builders<EmployeeFieldInfo>.Filter.Empty, updates, updateOption);
if (res.MatchedCount == 0)
{
throw new ResultException(Result.FromError("更新失败"));
}
return res; //3.修改整个嵌套文档
var collection = Collection;
var update = Builders<EmployeeFieldInfo>.Update.Set("Categories.$[i].Groups.$[j].EmployeeFields.$[k]", new EmployeeFieldInfo());
var updateOption = new UpdateOptions
{
ArrayFilters = new List<ArrayFilterDefinition<EmployeeFieldInfo>> {
new BsonDocument("i.Code", categoryCode),
new BsonDocument("j.Code", groupCode),
new BsonDocument(){
{ "k.Code", field.Code},
{ "k.IsDeleted",false}
}
}
};
var res = await collection.UpdateOneAsync(Builders<EmployeeFieldInfo>.Filter.Empty, update, updateOption);
if (res.MatchedCount == 0)
{
throw new ResultException(Result.FromError("更新失败"));
}
return res;
3.4.子文档删除
//删除PersonalInfo分类下、BasicInfoGroup分组下、Code为"Name"的员工字段子文档数据
db.EmployeeFieldInfos.updateOne({}, {
$pull: {
"Categories.$[i].Groups.$[j].EmployeeFields": {
"Code": "Name",
"IsActive": true
}
}
}, {
arrayFilters: [{
"i.Code": "PersonalInfo"
}, {
"j.Code": "BasicInfoGroup"
}]
})
var collection = Collection;
var update = Builders<EmployeeFieldInfo>.Update.Pull("Categories.$[i].Groups.$[j].EmployeeFields", new BsonDocument{
{"Code", "Name"},
{"IsActive", true}
});
var updateOption = new UpdateOptions
{
ArrayFilters = new List<ArrayFilterDefinition<EmployeeFieldInfo>> {
new BsonDocument("i.Code", categoryCode),
new BsonDocument("j.Code", groupCode),
new BsonDocument(){
{ "k.Code", field.Code},
{ "k.IsDeleted",false}
}
}
};
var res = await collection.UpdateOneAsync(Builders<EmployeeFieldInfo>.Filter.Empty, update, updateOption);
if (res.MatchedCount == 0)
{
throw new ResultException(Result.FromError("更新失败"));
}
return res;
【MongoDB】C# .Net MongoDB常用语法的更多相关文章
- MongoDB 安装详细教程 + 常用命令 + 与 Python 的交互
MongoDB 简介 MongoDB (名称来自 humongous/巨大无比的, 是一个可扩展的高性能,开源,模式自由,面向文档的NoSQL,基于 分布式 文件存储,由 C++ 语言编写,设计之初旨 ...
- 快速掌握mongoDB(一)——mongoDB安装部署和常用shell命令
1.mongoDB简介 mongoDB 是由C++语言编写的,是一种分布式的面向文档存储的开源nosql数据库.nosql是Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统 ...
- mongoDB(一)——mongoDB安装部署和常用shell命令
1.mongoDB简介 mongoDB 是由C++语言编写的,是一种分布式的面向文档存储的开源nosql数据库.nosql是Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统 ...
- MongoDB在Linux下常用优化设置
MongoDB在Linux下常用优化设置 以下是一些MongoDB推荐的常用优化设置.在生产环境下选取合适的参数值,例如预读值和默认文件描述符数目等,会对系统性能有很大的影响. 1.关闭数据库文件的 ...
- java 操作mongodb查询条件的常用设置
java操作mongodb进行查询,常用筛选条件的设置如下: 条件列表:BasicDBList condList = new BasicDBList(); 临时条件对象:BasicDBObject c ...
- mongodb数据库的一些常用命令列表
超级用户相关:use admin #增加或修改用户密码db.addUser(ixigua,'pwd') #查看用户列表db.system.users.find() #用户认证db.auth(ixigu ...
- 【数据库】Mean web开发 02-Windows下Mongodb安装配置及常用客户端管理工具
简介 Mean是JavaScript的全栈开发框架.更多介绍 用MongoDB实现持久数据的存储是Mean Web全栈开发中的一部分. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非 ...
- mongoDB & Nodejs 访问mongoDB (一)
最近的毕设需要用到mongoDB数据库,又把它拿出来再学一学,下盘并不是很稳,所以做一些笔记,不然又忘啦. 安装 mongoDB & mongoVUE mongoDB: https://www ...
- Mongodb 笔记01 MongoDB 简介、MongoDB基础知识、启动和停止MongoDB
MongoDB 简介 1. 易于使用:没有固定的模式,根据需要添加和删除字段更加容易 2. 易于扩展:MongoDB的设计采用横向扩展.面向文档的数据模型使它能很容易的再多台服务器之间进行分割.自动处 ...
- MongoDB Java连接---MongoDB基础用法(四)
MongoDB 连接 标准 URI 连接语法: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN ...
随机推荐
- Jumper Server 堡垒机搭建过程
Jumper Server 堡垒机搭建过程 背景说明 公司组织考核, 要对一套系统进行安全设置.有一个项目是使用堡垒机进行登录 堡垒机有多种用途,可以实现日志审计和安全设置等. 买商业设备的话太困难了 ...
- Oracle Linux7 与Oracle Linux8 下载rpm包的位置
Oracle Linux 7 http://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/index.html 索引页 http://yum.or ...
- 为什么Kubernetes和容器与机器学习密不可分?
原文出自infosecurity 作者:Rebecca James 京东云开发者社区编译 当前,数字化转型的热潮在IT领域发展的如火如荼,越来越多的企业投身其中,机器学习和人工智能等现代技术的融合在公 ...
- js分钟转化为小时并且以某个数字进行递增
有些时候,我们需要将分钟转为小时: 并且还有以一个数字进行递增: 呈现出[3,6,9,12,15,18]这样的递增形式 // 因为是递增,所以是相加: // 在使用+号的时候: // 注意两边都是数字 ...
- STM32CubeMX教程26 FatFs 文件系统 - W25Q128读写
1.准备材料 正点原子stm32f407探索者开发板V2.4 STM32CubeMX软件(Version 6.10.0) keil µVision5 IDE(MDK-Arm) ST-LINK/V2驱动 ...
- 【转载】基于Tablestore Timeline的IM(即时通讯)消息系统架构 - 架构篇
本文原作者:木洛,阿里云高级技术专家,内容有优化和修订,感谢原作者.原文链接:https://developer.aliyun.com/article/698301 IM全称是『Instant Mes ...
- 安装Docker填坑
从官网下载适合win10使用的docker,但是下载后,出现了各种坑,记录一下解决方式 1.docker想要正常启动,需要做以下的准备,开启 Windows 虚拟化和 Linux 子系统(WSL2), ...
- C# 中判断List集合是否为空
判断List集合是否为空,可以使用Count和Any,下面是其使用场景(别人总结)
- 从零开始配置 vim(5)——本地设置与全局设置
在前面的一系列文章中,我们介绍了使用 :noremap 进行键盘映射,使用 set 来设置选项和 vim 的变量.并且已经在配置文件中对他们进行了相关配置. 在介绍设置那一篇文章中我们提到了,lua ...
- C/C++ 提权与强制卸载DLL
权限提升 #include <Windows.h> #include <stdio.h> BOOL SetPrivilege(LPCTSTR lpszPrivilege, BO ...