コード例 #1
0
        public void AddFulFillmentSafe(SalesOrderFulfillmentRequest request)
        {
            var database   = client.GetDatabase(DatabaseName);
            var collection = database.GetCollection <SalesOrderDocument>(CollectionName);

            var fb         = Builders <SalesOrderDocument> .Filter;
            var lineItemFb = Builders <SalesOrderDocument.Fulfillment> .Filter;

            var filter = fb.Eq(s => s.OrderId, request.OrderId) &
                         fb.ElemMatch(s => s.Fulfillments, lineItemFb.Ne(s => s.FulfillmentId, request.FulfillmentId));

            var updateDefinition = Builders <SalesOrderDocument> .Update;

            var updateItems = new List <UpdateDefinition <SalesOrderDocument> > {
                updateDefinition.Push(s => s.Fulfillments, new SalesOrderDocument.Fulfillment {
                    FulfillmentId   = request.FulfillmentId,
                    FulfillmentDate = request.Date,
                    LineItems       = request.LineItems.Select(s => new SalesOrderDocument.Fulfillment.FulfillmentLineItem {
                        ProductId = s.ProductId,
                        Quantity  = s.Quantity
                    }).ToList()
                })
            };

            var result = collection.UpdateOne(filter, updateDefinition.Combine(updateItems));

            Debug.WriteLine(JsonConvert.SerializeObject(result));
        }
コード例 #2
0
        public void AddFulFillment(SalesOrderFulfillmentRequest request)
        {
            var database   = client.GetDatabase(DatabaseName);
            var collection = database.GetCollection <SalesOrderDocument>(CollectionName);

            var query = collection.AsQueryable();

            var order = query.Single(s => s.OrderId == request.OrderId);

            order.Fulfillments.Add(new SalesOrderDocument.Fulfillment
            {
                FulfillmentId   = request.FulfillmentId,
                FulfillmentDate = request.Date,
                LineItems       = request.LineItems.Select(s => new SalesOrderDocument.Fulfillment.FulfillmentLineItem
                {
                    ProductId = s.ProductId,
                    Quantity  = s.Quantity
                }).ToList()
            });

            var fb     = Builders <SalesOrderDocument> .Filter;
            var filter = fb.Eq(s => s.OrderId, request.OrderId);

            var result = collection.ReplaceOne(filter, order);

            Debug.WriteLine(JsonConvert.SerializeObject(result));
        }