sql >> Base de Datos >  >> NoSQL >> MongoDB

¿Cómo hacer un upsert con MongoDB 2.0?

Pase una instancia de UpdateOptions como parámetro de opciones en UpdateOneAsync(filter, update, options) , por ejemplo:

collection.UpdateOneAsync(p => p.Id == user.Id, 
    Builders<User>.Update.Set(p => p.Name, "John"), 
    new UpdateOptions { IsUpsert = true });

EDITAR

Para reemplazar el documento, llame a ReplaceOneAsync en cambio:

collection.ReplaceOneAsync(p => p.Id == user.Id, 
    user, 
    new ReplaceOptions { IsUpsert = true });