Function MongoCollection.replaceOne
Replaces at most single document within the collection based on the filter.
UpdateResult replaceOne(T, U)
(
T filter,
U replacement,
ReplaceOptions options
) @safe;
UpdateResult replaceOne(T, U)
(
T filter,
U replacement,
UpdateOptions options = UpdateOptions .init
) @safe;
It's recommended to use the ReplaceOptions overload, but UpdateOptions can be used as well. Note that the extra options inside UpdateOptions may have no effect, possible warnings for this may only be handled by MongoDB.
See Also
Standards
https://www.mongodb.com/docs/manual/reference/command/update/
Example
import vibe .db .mongo .mongo;
void test(BsonObjectID id)
{
auto coll = connectMongoDB("127.0.0.1") .getCollection("test");
// replaces the existing document with _id == id to `{_id: id, name: "Bob"}`
// or if it didn't exist before this will just insert, since we enabled `upsert`
ReplaceOptions options;
options .upsert = true;
coll .replaceOne(
["_id": id],
[
"_id": Bson(id),
"name": Bson("Bob")
],
options
);
}