MongoDB C # Driver - 2 databases hit instead of one when doing UPSERT
I am using the latest official C # wrapper for MongoDB (2.0.0).
(mongo db version v3.0.3)
My model:
public class MovieRent
{
public int MovieCode { get; set; }
public int RentStatus { get; set; }
}
My method updates the rental status of the movie and if the video dose does not exist, it injects a new document (upsert):
public async Task UpdateMovieRent(MovieRent rent)
{
var rents = _Database.GetCollection<MovieRent>("movierents");
var builder = Builders<MovieRent>.Update;
var update = builder.Set(m => m.RentStatus, rent.RentStatus);
var options = new UpdateOptions { IsUpsert = true };
await rents.UpdateOneAsync<MovieRent>(m => m.MovieCode == rent.MovieCode, update, options);
}
When looking at the server trace information (I run mongod.exe -v), I see two commands being executed.
The first one is:
update movies.movierents query: { MovieCode : "3" } update: { $set: { RentStatus: "1" } }
Second:
command movies.$cmd command: update {
update: "movierents",
ordered: true,
updates: [
{
q: { MovieCode: "3" },
u: { $set: { RentStatus: "1" } },
upsert: true
} ] }
This looks like a lot of db deletes (twice as many (- :)) as I can get the same update with only one command via the mongo shell, e.g .:
db.movierents.update(
{ MovieCode:"3" },
{
$set: { RentStatus: "1" }
},
{ upsert: true }
)
This actually results in only one entry in the mongo trace instead of two when using the C # driver.
Is this how the C # driver works by design or am I missing something here?
+3
source to share
No one has answered this question yet
Check out similar questions: