MongoCollection.replaceOne

Replaces at most single document within the collection based on the filter.

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.

  1. UpdateResult replaceOne(T filter, U replacement, ReplaceOptions options)
    struct MongoCollection
    @safe
    UpdateResult
    replaceOne
    (
    T
    U
    )
    (,,
    ReplaceOptions options
    )
  2. UpdateResult replaceOne(T filter, U replacement, UpdateOptions options)

Examples

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
	);
}

See Also

Meta