VB net & Mongo: 在 LINQ 中使用 where 子句导致错误 "Unsupported where clause: (Boolean)Operators.CompareObjectLess"

VB net & Mongo: Using where clause with LINQ causes error "Unsupported where clause: (Boolean)Operators.CompareObjectLess"

我在 MongoDB 中有一个集合,我在 VB 网络中使用 MongoDB 驱动程序。我想根据条件更新多个文档。 为此,我想使用 LINQ,但 select 导致错误,我不知道如何修复它。

代码如下:

Dim update_for As UpdateBuilder
Dim query_for As IMongoQuery
Dim coll_for = db.GetCollection(Of MyClass)("collection_1")
Dim queryMun = (From a In coll_for _
                Where (a.field_1 < 10000) _
                Select a)

For Each emp In queryMun
    query_for = Query.EQ("_id", emp.Id)
    update_for = Update.Set("field_1", BsonValue.Create("0" + emp.field_1))
    coll.Update(query_for, update_for, opts)
Next

执行de For Each语句时抛出异常:Unsupported where clause: (Boolean)Operators.CompareObjectLess(a.field_1, 10000 , 真).

我做错了什么?

非常感谢您的帮助。

我认为错误很明显:

您不能在 WHERE 子句中使用小于“<”运算符,因为它不受支持。

我找到了一种根据属性本身的值进行更新的方法。我想做的是在属性值的开头加一个“0”,比如如果field_1=4567,更新后field_1='04567'.

代码如下:

Dim update_for As UpdateBuilder
Dim query_for As IMongoQuery
Dim opts = New MongoUpdateOptions
opts.Flags = UpdateFlags.Multi
Dim coll_for = db.GetCollection(Of MyLINQClass)("collection_1")

Dim queryMun2 As New QueryDocument
Dim query_1 = Query.LT("field_1", MongoDB.Bson.BsonValue.Create(10000))
queryMun2.AddRange(query_1.ToBsonDocument)

Dim queryMun = coll_for.Find(queryMun2)

For Each emp In queryMun
    query_for = Query.EQ("_id", emp.Id)
    update_for = Update.Set("field_1", BsonValue.Create("0" + emp.FField_1.ToString))
    coll.Update(query_for, update_for, opts)
Next

这里是 MyLINQClass 的定义:

Public Class MyLINQClass
    <BsonElementAttribute("_id")> _
    Public Property Id() As ObjectId

    <BsonElementAttribute("field_1")> _
    Public Property FField_1() As Object
End Class