Thomas Thomas - 5 months ago 46
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:


Can it be done with an update / pullall?

Answer Source

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


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.