Arrays
Push new item - $push
The $push operator is used to append a new item to an array. To push a new item create an UpdateDefinition<T> by defining the array field and the new item to push.
Builders<T>.Update
.Push(doc => doc.<array-field>, <array-item>);The sample pushes a new VisitedCountry in the VisitedCountries array of a Traveler document.
var collection = database
.GetCollection<Traveler>(Constants.TravelersCollection);
var firstTraveler = Builders<Traveler>.Filter.Empty;
// create a visited country item
var visitedCountry = RandomData
.GenerateVisitedCountries(1).First();
visitedCountry.Name = "South Korea";
visitedCountry.TimesVisited = 5;
visitedCountry.LastDateVisited = DateTime.UtcNow.AddYears(5);
// create the update definition
var pushCountryDefinition = Builders<Traveler>
.Update.Push(t => t.VisitedCountries, visitedCountry);
var addNewVisitedCountryResult = await collection
.UpdateOneAsync(firstTraveler, pushCountryDefinition);var bsonCollection = database
.GetCollection<BsonDocument>(Constants.TravelersCollection);
var bsonFirstUser = Builders<BsonDocument>.Filter.Empty;
var bsonVisitedCountry = RandomData.GenerateVisitedCountries(1).First();
visitedCountry.Name = "North Korea";
visitedCountry.TimesVisited = 5;
visitedCountry.LastDateVisited = DateTime.UtcNow.AddYears(5);
var bsonPushCountryDefinition = Builders<BsonDocument>.Update
.Push("visitedCountries", visitedCountry.ToBsonDocument());
var bsonAddNewVisitedCountryResult = await bsonCollection
.UpdateOneAsync(bsonFirstUser, bsonPushCountryDefinition);Push multiple items
To push multiple items in a array field use the UpdateDefinitionBuilder<T>.PushEach method. The method creates a $push => $each operation and pushes all array items to the array field.
The sample adds 10 new VisitedCountry items in the VisitedCountries array field.
Push items to a queue
PushEach method accepts an optional parameter slice which when set properly can provide a queue functionality. Assume there's a SocialAccount document containing an array of notifications but you wish to always contain the last 2 inserted.
The sample tries to add 4 new notifications in the lastNotifications array field using a $slice:-2 operation. The resulting document contains only the last 2 notifications added.
Pull items - $pull
To remove items that match a specified condition use the Builders.Update.PullFilter method.
The sample removes two string values ("FIFA 20", "NBA 2K17") from the PcGames string array field. The empty filter ensures to remove the items for all documents in the collection.
Pull items from multiple arrays
To remove items from multiple array fields at the same time use the Builders.Update .Combine method to combine 2 or more update definitions.
The sample removes two string values from the PcGames array field and one from the XboxGames.
Pull embedded items from array
To remove nested documents from an array field create an UpdateDefinition with the specified condition to be applied on the documents to be removed. Assuming T is the type of the root document which contains an array field having E type of documents, the syntax is the following.
The sample removes VisitedCountry documents from the VisitedCountries array field of Traveler documents based on the VisitedCountry.TimesVisited value.
Update matched array document
To update a specific array element without knowing its position in the array, you can use the $ positional operator. The driver can build such a query by translating the array[-1] as a positional operator.
The sample sets the Name value for all matched array VisitedCountry elements having Name = Greece and TimesVisited = 3.
Normally, based on the $elemMatch operator, the $set operator should have been applied to the root document(s) but the $ position operator causes the update to be applied on the matched document instead
Update multiple elements
To update multiple array elements that match a specified condition, you need to use one or more ArrayFilterDefinition filters as the 3rd argument in the UpdateOne or UpdateMany methods.
Assume that you have a post which contains a list of comments.
The sample hides the comments that have votes greater or equal to 3.
There update operation has 3 distinct parts:
The filter definition - in the sample it's a specific post matched by its _id but it could be anything
The update definition - the sample defines that the matched elements in the comments array field should set their hidden value to true. This is defined using the $ positional operator and the elem identifier for each matching document
The ArrayFilters which defines how elem array elements are matched, in other words, which are the elements to be updated
Last updated