The $slice operator is used to return a subset of an array. The Enumerable.Take method can be used to create a $slice operation on a array field.
Method call
Description
Take(1)
Returns the first element
Take(N)
Returns the first N elements
Take(-1)
Returns the last element
Take(-N)
Returns the last N elements
Get first N elements
The sample returns Traveler documents but only include the first element of their VisitedCountries array field.
Slice.cs
var travelersQueryableCollection = tripsDatabase .GetCollection<Traveler>(Constants.TravelersCollection) .AsQueryable();var sliceQuery =from t in travelersQueryableCollectionselectnew {t.Name, visitedCountries =t.VisitedCountries.Take(1) // slice here };var sliceQueryResults =awaitsliceQuery.ToListAsync();
publicclassTraveler{ [BsonId]publicObjectId Id { get; set; }publicstring Name { get; set; }publicint Age { get; set; }publicList<string> Activities { get; set; }publicList<VisitedCountry> VisitedCountries { get; set; }}
The same result can be achieved using a ProjectionDefinition.
var sliceProjection =Builders<Traveler>.Projection .Expression(u =>new { name =u.Name, visitedCountries =u.VisitedCountries.Take(1) });var sliceProjectionResults =await travelersCollection .Find(Builders<Traveler>.Filter.Empty) .Project(sliceProjection) .ToListAsync();
Get last N elements
The following sample returns the Traveler documents but only the last 2 visited countries included.
Slice.cs
var sliceQueryTwoLastCountries =from t in travelersQueryableCollectionselectnew { t.Name, visitedCountries =t.VisitedCountries.Take(-2) };var sliceQueryTwoLastCountriesResults =await sliceQueryTwoLastCountries .ToListAsync();
publicclassTraveler{ [BsonId]publicObjectId Id { get; set; }publicstring Name { get; set; }publicint Age { get; set; }publicList<string> Activities { get; set; }publicList<VisitedCountry> VisitedCountries { get; set; }}
Pagination
Slice can be combined with the Skip method and provide full pagination functionality. The following sample skips the first 2 VisitedCountries array elements and returns the next 3.
var travelersQueryableCollection = tripsDatabase .GetCollection<Traveler>(Constants.TravelersCollection) .AsQueryable();var sliceWithSkipQuery =from t in travelersQueryableCollectionselectnew { t.Name, visitedCountries =t.VisitedCountries .Skip(2).Take(3) };var sliceWithSkipQueryResults =awaitsliceQuery.ToListAsync();
publicclassTraveler{ [BsonId]publicObjectId Id { get; set; }publicstring Name { get; set; }publicint Age { get; set; }publicList<string> Activities { get; set; }publicList<VisitedCountry> VisitedCountries { get; set; }}
Filter operator - $filter
The $filter operator is used to match and return array elements that fulfill the specified condition. The Enumerable.Where method can be used to create the condition.
The sample returns Traveler documents with their VisitedCountries array field containing only the countries that have been visited once.
ComparisonOperators.cs
var travelersQueryableCollection = tripsDatabase .GetCollection<Traveler>(Constants.TravelersCollection) .AsQueryable();var filterQuery =from t in travelersQueryableCollectionselectnew {t.Name, visitedCountries =t.VisitedCountries .Where(c =>c.TimesVisited==1) };var filterQueryResults =awaitfilterQuery.ToListAsync();
The sample creates a projection stage to return the total for each order, with total=priceβquantity
Multiply.cs
var ordersCollection = tripsDatabase .GetCollection<Order>(Constants.OrdersCollection);var multiplyQuery =from o inordersCollection.AsQueryable()selectnew// creates a projection stage {o.OrderId, total =o.Quantity*o.Price// $multiply field };var multiplyQueryResults =awaitmultiplyQuery.ToListAsync();