wuyts
wuyts

Reputation: 105

delete element from document in MongoDB using C#

I have a mongoDB where all the documents have this structure:

{
"_id" : ObjectId("522489bdfc346a1464659634"),
"Flash_point" : 105,
"Boiling_point" : 112,
"Melting_point" : 41}

I have no idea how to delete "Flash_point"-element from a document in C#

Afterwards, the document should look like this:

{
"_id" : ObjectId("522489bdfc346a1464659634"),
"Boiling_point" : 112,
"Melting_point" : 41}

Many thanks!

Upvotes: 3

Views: 2540

Answers (1)

wuyts
wuyts

Reputation: 105

After searching on the web for a while, I found it myself.

For those who want to know my solution:

for (int j = 0 ; j < idlist.Count ; j++)
{
    var queryDeleteValue = new QueryDocument("_id", idlist[j]);
    var update = Update.Unset("Flash_point");
    collectionInput.Update(queryDeleteValue, update);
}

So, first of all, I have a query-variable that selects the right document. Then I make an update variable where I unset the element 'Flash_point'. The last step is to do the actual update (with the parameters 'queryDeleteValue' and 'update').

Upvotes: 2

Related Questions