Thomas Thomas - 10 months ago 81
C# Question

MongoDB C# - Update to clear a list

If I have an object with a list:

List<string> myList { get; set; }


How can I do a mongodb update to do the equivalent of:

myList.Clear();


Can it be done with an update / pullall?

Answer Source

db.collection.update({}, { $set : {'myArray': [] }} , {multi:true} )

or

db.collection.update({}, { $pull : { 'myArray': {} }}, {multi:true} )

will both do the job but $set is the proper way of doing this since this operation doesn't do any calculations in the array like $pull does. This is all relevant if you must hold on to the empty array.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download