B4J Question MongoDB update and upsert

lucdrb

Active Member
Licensed User
Longtime User
Hi,

Is it possible to update and upsert multiple document with the JMongo Library?

How should I use the Update (Filter As Map, Update As Map) As UpdateResult from the MongoCollection to insert these options?

Thank in advance.

Luc
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
MongoCollection.Update will update all matching documents.

It is possible to set upsert to true with this untested code:
B4X:
Sub UpdateOrInsert (Collection As MongoCollection, Filter As Map, Update As Map) As UpdateResult
   Dim jco As JavaObject = Collection
   Dim utils As JavaObject
   utils.InitializeStatic("anywheresoftware.mongo.MongoUtils")
   Dim options As JavaObject
   options.InitializeNewInstance("com.mongodb.client.model.UpdateOptions", Null)
   options.RunMethod("upsert", Array(True))
   Return jco.RunMethod("updateMany", Array( _
       utils.RunMethod("MapToBson", Array(Filter)), _
       utils.RunMethod("MapToBson", Array(Update)), _
       options))
End Sub
 
Upvote 0
Top