vibe.d beta banner
get vibe.d
0.10.0

Asynchronous I/O that doesn’t get in your way, written in D

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

https://www.mongodb.com/docs/manual/reference/method/db.collection.replaceOne/#mongodb-method-db.collection.replaceOne

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

Authors

Sönke Ludwig

Copyright

© 2012-2016 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.