BulkWrite 动态文档在 MongoDB 中使用 VB.NET



潜伏15年后的第一篇文章。我将非常感谢任何帮助。 我的需求很简单:使用JSON数据,将插入更新到mongodb。有很多记录,这个过程会一遍又一遍地重复,所以看起来 BulkWrite 就是我想要的。记录的 id 将保持不变,所有其他字段都需要在每次运行期间更新插入(例如,days_employed将经常更新(。VB是必须的。

到目前为止,我的代码如下:

Dim client As MongoClient
Dim db As IMongoDatabase
client = New MongoClient("mongodb://localhost/")
db = client.GetDatabase("db")

Dim collection As IMongoCollection(Of BsonDocument) = db.GetCollection(Of BsonDocument)("employees")
Dim documents As New List(Of BsonDocument)
''CREATE 10 documents to insert into the collection
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i As Integer = 1 To 10
Dim emp As BsonDocument = New BsonDocument
With emp
.Add("_id", "x" & i) 'Guid.NewGuid().ToString)
.Add("name", "George Jones")
.Add("first_name", "George")
.Add("days_employed", "24")
.Add("job_desc", "Coder")
.Add("location", "Istanbul")
End With
documents.Add(emp)
Next
'bulkwrite documents into collection
collection.BulkWrite(documents.[Select](Function(d) New InsertOneModel(Of BsonDocument)(d)))

但正如你所看到的,这只是插入,它不会更新。我想我需要使用UpdateOneModel而不是InsertOneModel,但是UpdateOneModel需要过滤器和更新参数。我不知道如何动态创建更新参数。我发现的每个示例都将更新值硬编码到示例中,但就我而言,该数据来自另一个来源。任何帮助将不胜感激。如果我不清楚,我深表歉意,并在必要时尝试澄清。如果有另一种完全不同的方式来完成同样的任务,我也非常愿意接受。我来自SQL Server,我发现它更简单,但需要处理大量数据(每天数百万条记录(,并希望查看mongodb的性能是否有改进。没有很多 .NET 代码示例,VB 示例几乎为零。

提前感谢!

编辑: 添加了将使用的 JSON 示例:

{
"name": "George Jones",
"status": "SUCCESS",
"days_employed": 28,
"appointments": {
"2020-03-12:0": {
"2pm": [
{
"apptWith": "Alex Thompson",
"MaxLength": 90,
"description": "Product Development",
"meetingLocation": "New York"
}
],
"4pm": [
{
"apptWith": "Paul Jones",
"MaxLength": 60,
"description": "Accounting",
"meetingLocation": "New York"
}
]
},
"2020-03-14:7": {
"1130am": [
{
"apptWith": "Mike Rogers",
"MaxLength": 90,
"description": "Product Development",
"meetingLocation": "San Diego"
}
],
"230pm": [
{
"apptWith": "Felix Henderson",
"MaxLength": 30,
"description": "Accounting",
"meetingLocation": "San Diego"
}
]
}
},
"appointmentRequests": {
"2020-03-10:0": {
"8am": [
{
"apptWith": "Jose Guitierez",
"MaxLength": 60,
"description": "Product Launch",
"meetingLocation": "Orlando"
}
],
"3pm": [
{
"apptWith": "Ronald Clubman",
"MaxLength": 60,
"description": "Marketing",
"meetingLocation": "New York"
}
]
},
"2020-03-11:7": {
"1130am": [
{
"apptWith": "Phil Norton",
"MaxLength": 90,
"description": "Supply Chain",
"meetingLocation": "New York"
}
],
"130pm": [
{
"apptWith": "Felix Henderson",
"MaxLength": 60,
"description": "Manufacturing",
"meetingLocation": "San Francisco"
}
]
}
}
}

这似乎可以完成工作:

Imports System.Net.Http
Imports MongoDB.Bson
Imports MongoDB.Bson.Serialization
Imports MongoDB.Driver
Imports System.Text.RegularExpressions
Module Program
Private ReadOnly collection As IMongoCollection(Of BsonDocument) =
New MongoClient("mongodb://localhost/").
GetDatabase("test").
GetCollection(Of BsonDocument)("employees")
Sub Main(args As String())
Dim docs = GetDocs()
Dim models = New List(Of ReplaceOneModel(Of BsonDocument))()
Dim i = 1
While i <= docs.Length OrElse i = 10
Dim doc = docs(i - 1)
Sanitize(doc)
doc.Add("_id", "x" & i.ToString()) 'make sure to generate the same id everytime for upsert to work
Dim upsert = New ReplaceOneModel(Of BsonDocument)(
filter:=Builders(Of BsonDocument).Filter.Eq(Of String)("_id", doc.GetValue("_id")),
replacement:=doc) With {.IsUpsert = True}
models.Add(upsert)
i += 1
End While
collection.BulkWrite(models)
End Sub
Private Function GetDocs() As BsonDocument()
Using client = New HttpClient()
Dim json = client.GetStringAsync("https://pastebin.com/raw/QtMquCps").GetAwaiter().GetResult()
Return BsonSerializer.Deserialize(Of BsonArray)(json).[Select](Function(p) p.AsBsonDocument).ToArray()
End Using
End Function
Private Sub Sanitize(ByRef doc As BsonDocument)
doc = BsonDocument.Parse(
Regex.Replace(
doc.ToString(),
"""w+.w+""s*:",
Function(m) m.Value.Replace(".", ":")))
End Sub
End Module

附带说明一下,必须编写此VB代码使我现在更加欣赏C#:-(

最新更新