更新MongoDB的文档在VB.NET与C#驱动程序驱动程序、文档、MongoDB、NET

2023-09-04 05:57:29 作者:无心者

我有更新使用VB.NET和放大器MongoDB中的文件的问题; C#的驱动程序。

我有code返回我想要更新的文档,但我不知道的语法继续

 进口MongoDB.Bson
进口MongoDB.Driver
进口MongoDB.Driver.Builders

昏暗的蒙戈作为MongoServer = MongoServer.Create()
mongo.Connect()
昏暗DB = mongo.GetDatabase(数据库)
昏暗的股票= db.GetCollection(中BsonDocument)(股票)

昏暗getDocument =新QueryDocument(产品名称,测试)
 

解决方案

使用的coll.update方法。例如以设置一个字段的值

 昏暗的客户端=新MongoClient()
昏暗DB = client.GetServer()。GetDatabase(测试)
昏暗的科尔= db.GetCollection(vbtest)
昏暗productQuery = Query.EQ(产品名称,测试)
昏暗updateStmt = Update.Set(成本,3000)
coll.Update(productQuery,updateStmt)
 
VB.NET

上的方法的详细信息可以在 http://api.mongodb.org/找到CSHARP / 1.8.1 / 。此外,我会建议通过教​​程将在 http://docs.mongodb.org/ecosystem/drivers / CSHARP / 。该例子是在C#中,虽然

I am having problems updating a document in MongoDB using VB.NET & the C# Driver.

I have code returning the document I wish to update but I'm not sure of the syntax to continue

Imports MongoDB.Bson
Imports MongoDB.Driver
Imports MongoDB.Driver.Builders

Dim mongo As MongoServer = MongoServer.Create()
mongo.Connect()
Dim db = mongo.GetDatabase("database")
Dim stock = db.GetCollection(Of BsonDocument)("stock")

Dim getDocument = New QueryDocument("productName", "test")

解决方案

Use the coll.update method. e.g. to set the value of a field

Dim client = New MongoClient()
Dim db = client.GetServer().GetDatabase("test")
Dim coll = db.GetCollection("vbtest")
Dim productQuery = Query.EQ("productName", "test")
Dim updateStmt = Update.Set("cost", 3000)
coll.Update(productQuery, updateStmt)

More information on the methods can be found at http://api.mongodb.org/csharp/1.8.1/. Also I would recommend going through the tutorials at http://docs.mongodb.org/ecosystem/drivers/csharp/. The examples are in C# though