Mongodb 如何使用 C# 驱动程序更新多个文档
Mongodb How to Update many documents using C# driver
请多多帮助。
我从 xml 解析服务获取了文档列表并尝试在数据库中更新它。
我创建了类似 .
的 fiter builder
var filter = Builders<T>.Filter.In("Id", List<T>);
并像这样更新生成器。
var update = Builders<T>.Update.Set("T.Property", List<T> )
并使用 UpdateManyAsync() 更新数据库中的文档,但更改不适用。
如何在 1 个步骤中更新文档?
您好,这是一个使用 .NET core 3.1 控制台应用程序的示例。
这是 csproj
文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.13.1" />
</ItemGroup>
</Project>
这是 Program.cs
文件中的代码:
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MongoUpdateMany
{
public static class Program
{
public static async Task Main(string[] args)
{
const string databaseName = "test";
const string collectionName = "students";
var client = new MongoClient();
var database = client.GetDatabase(databaseName);
var collection = database.GetCollection<Student>(collectionName);
// just to be sure the test data are clean, nothing to do with the update sample
await database.DropCollectionAsync(collectionName).ConfigureAwait(false);
// create a bunch of students
var id = 1;
var enrico = new Student()
{
Name = "Enrico",
Id = id++,
IsActive = false
};
var luca = new Student
{
Name = "Luca",
Id = id++,
IsActive = false
};
var giulia = new Student
{
Name = "Giulia",
Id = id++,
IsActive = true
};
// fill the collection
await collection.InsertManyAsync(new List<Student> { enrico, giulia, luca }).ConfigureAwait(false);
// update many
var ids = new List<int> { enrico.Id, luca.Id };
var filter = Builders<Student>
.Filter
.In(x => x.Id, ids);
var update = Builders<Student>
.Update
.Set(x => x.IsActive, true);
await collection.UpdateManyAsync(filter, update).ConfigureAwait(false);
// verify updating the docs worked
await collection
.Find(student => ids.Contains(student.Id))
.ForEachAsync(student => Console.WriteLine($"Name: {student.Name} IsActive: {student.IsActive}"))
.ConfigureAwait(false);
Console.WriteLine();
Console.WriteLine("Press enter to close...");
Console.ReadLine();
}
}
public class Student
{
[BsonId]
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
}
这里有一些有用的链接,可以了解如何使用 mongodb 的官方 C# 驱动程序:
- driver documentation
- free course from the Mongo university。 我强烈建议你学习这门课程:Mongo 大学目录中提供的课程质量非常高。
请多多帮助。
我从 xml 解析服务获取了文档列表并尝试在数据库中更新它。
我创建了类似 .
的 fiter buildervar filter = Builders<T>.Filter.In("Id", List<T>);
并像这样更新生成器。
var update = Builders<T>.Update.Set("T.Property", List<T> )
并使用 UpdateManyAsync() 更新数据库中的文档,但更改不适用。
如何在 1 个步骤中更新文档?
您好,这是一个使用 .NET core 3.1 控制台应用程序的示例。
这是 csproj
文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.13.1" />
</ItemGroup>
</Project>
这是 Program.cs
文件中的代码:
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MongoUpdateMany
{
public static class Program
{
public static async Task Main(string[] args)
{
const string databaseName = "test";
const string collectionName = "students";
var client = new MongoClient();
var database = client.GetDatabase(databaseName);
var collection = database.GetCollection<Student>(collectionName);
// just to be sure the test data are clean, nothing to do with the update sample
await database.DropCollectionAsync(collectionName).ConfigureAwait(false);
// create a bunch of students
var id = 1;
var enrico = new Student()
{
Name = "Enrico",
Id = id++,
IsActive = false
};
var luca = new Student
{
Name = "Luca",
Id = id++,
IsActive = false
};
var giulia = new Student
{
Name = "Giulia",
Id = id++,
IsActive = true
};
// fill the collection
await collection.InsertManyAsync(new List<Student> { enrico, giulia, luca }).ConfigureAwait(false);
// update many
var ids = new List<int> { enrico.Id, luca.Id };
var filter = Builders<Student>
.Filter
.In(x => x.Id, ids);
var update = Builders<Student>
.Update
.Set(x => x.IsActive, true);
await collection.UpdateManyAsync(filter, update).ConfigureAwait(false);
// verify updating the docs worked
await collection
.Find(student => ids.Contains(student.Id))
.ForEachAsync(student => Console.WriteLine($"Name: {student.Name} IsActive: {student.IsActive}"))
.ConfigureAwait(false);
Console.WriteLine();
Console.WriteLine("Press enter to close...");
Console.ReadLine();
}
}
public class Student
{
[BsonId]
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
}
这里有一些有用的链接,可以了解如何使用 mongodb 的官方 C# 驱动程序:
- driver documentation
- free course from the Mongo university。 我强烈建议你学习这门课程:Mongo 大学目录中提供的课程质量非常高。