B4J Question MongoDB InsertOrReplace Example?

LWGShane

Well-Known Member
Licensed User
Longtime User
Are there or will there be any sort of example for the InsertOrReplace method in the MongoDB Remote Connector?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Lets say that you have first inserted this document:
B4X:
manager.InsertOne("test",CreateMap("first name": "Erel"), Null)

Later you decide that you want to add the last name.
B4X:
manager.InsertOne("test",CreateMap("first name": "Erel", "last name": "Uziel"), Null)
This will not work as there will now be two documents.
To properly update the first document you need:
B4X:
manager.InsertOrReplace("test", CreateMap("first name": "Erel"), CreateMap("first name": "Erel", "last name": "Uziel"), Null)

If you are implementing a key value store then you always use InsertOrReplace:
B4X:
manager.InsertOrReplace("test", CreateMap("_id": "the key goes here"), CreateMap("_id": "the key goes here", "other field": "other value"), Null)
 
Upvote 0
Top