Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent "_t" field from appearing when Upserting into MongoDB?

Tags:

c#

mongodb

I have an application which makes Upserts into a MongoDB database, using the c# driver for MongoDB. When I call the Update function, I can't specify the type I want to update, and then a _t field is inserted with the type of the element.

Here's the code I use to upsert:

collection.Update(
    Query.EQ("key", item.Key),
    Update.Replace(item),
    UpdateFlags.Upsert
);

Here's the result:

mongodbscreenshot

This does not happen when I do the initial inserts, since I can specify the type.

How can I make Upserts without inserting the _t field?

[Edit] That's the code I use for inserting:

collection.InsertBatch(ItemType, items);
like image 638
Joanvo Avatar asked Sep 30 '22 14:09

Joanvo


1 Answers

You can pass the ItemType in the Update.Replace method:

collection.Update(
    Query.EQ("key", item.Key),
    Update.Replace(ItemType, item),
    UpdateFlags.Upsert
);
like image 146
David Avatar answered Oct 10 '22 09:10

David