private async Task MultiplySamples() { var tripsDatabase = Client.GetDatabase(Constants.SamplesDatabase); var ordersCollection = tripsDatabase.GetCollection <Order>(Constants.OrdersCollection); var ordersBsonCollection = tripsDatabase.GetCollection <BsonDocument>(Constants.OrdersCollection); #region Prepare data await ordersCollection.InsertManyAsync(RandomData.GenerateOrders(100)); #endregion #region Linq var multiplyQuery = from o in ordersCollection.AsQueryable() select new { o.OrderId, total = o.Quantity * o.Price }; var multiplyQueryResults = await multiplyQuery.ToListAsync(); Utils.Log(multiplyQueryResults.Select(r => r.ToBsonDocument())); #endregion #region Shell commands #if false #endif #endregion }
private async Task ElementOperatorsSamples() { var database = Client.GetDatabase(Constants.SamplesDatabase); var collection = database.GetCollection <Order>(Constants.OrdersCollection); var bsonCollection = database.GetCollection <BsonDocument>(Constants.OrdersCollection); #region Prepare data var orders = RandomData.GenerateOrders(1000); await collection.InsertManyAsync(orders); #endregion #region Typed classes commands var lotNumberFilter = Builders <Order> .Filter.Exists(o => o.LotNumber, exists : true); var ordersWithLotNumber = await collection.Find(lotNumberFilter).ToListAsync(); Utils.Log($"{ordersWithLotNumber.Count} orders have Lot number"); // use exists in nested property var shippedOrdersFilter = Builders <Order> .Filter.Exists(o => o.ShipmentDetails.ShippedDate); var shippedOrders = await collection.Find(shippedOrdersFilter).ToListAsync(); Utils.Log($"{shippedOrders.Count} orders have been already shipped"); var typeFilter = Builders <Order> .Filter.Type(o => o.ShipmentDetails.ShippedDate, BsonType.DateTime); shippedOrders = await collection.Find(typeFilter).ToListAsync(); var nullContactPhoneFilter = Builders <Order> .Filter .Type(o => o.ShipmentDetails.ContactPhone, BsonType.Null); var nullContactPhoneOrders = await collection .Find(nullContactPhoneFilter).ToListAsync(); Utils.Log($"{nullContactPhoneOrders.Count} orders don't contain Contact Phone number"); #endregion #region BsonDocument commands var bsonLotNumberFilter = Builders <BsonDocument> .Filter.Exists("lotNumber", exists : true); var bsonOrdersWithLotNumber = await collection.Find(lotNumberFilter).ToListAsync(); var bsonShippedOrdersFilter = Builders <BsonDocument> .Filter .Exists("shipmentDetails.shippedDate"); var bsonShippedOrders = await bsonCollection.Find(bsonShippedOrdersFilter).ToListAsync(); var bsonTypeFilter = Builders <BsonDocument> .Filter.Type("shipmentDetails.shippedDate", BsonType.DateTime); bsonShippedOrders = await bsonCollection.Find(bsonTypeFilter).ToListAsync(); var bsonNullContactPhoneFilter = Builders <BsonDocument> .Filter .Type("shipmentDetails.contactPhone", BsonType.Null); var bsonNullContactPhoneOrders = await bsonCollection .Find(bsonNullContactPhoneFilter).ToListAsync(); #endregion #region Shell commands #if false db.invoices.find({ lotNumber: { $exists: true } })