Exemple #1
0
        /// <summary>
        /// Specify an array filter to target nested entities for updates with the .Modify() method (use multiple times if needed).
        /// </summary>
        /// <param name="filter">{ 'x.SubProp': { $gte: 123 } }</param>
        /// <returns></returns>
        public Update <T> WithArrayFilter(string filter)
        {
            ArrayFilterDefinition <T> def = filter;
            var arrFilters = options.ArrayFilters == null ? new List <ArrayFilterDefinition>() : options.ArrayFilters.ToList();

            arrFilters.Add(def);
            options.ArrayFilters = arrFilters;
            return(this);
        }
        /// <summary>
        /// Specify an array filter to target nested entities for updates (use multiple times if needed).
        /// </summary>
        /// <param name="filter">{ 'x.SubProp': { $gte: 123 } }</param>
        public Update <T> WithArrayFilter(string filter)
        {
            ArrayFilterDefinition <T> def = filter;

            options.ArrayFilters =
                options.ArrayFilters == null
                ? new List <ArrayFilterDefinition>()
            {
                def
            }
                : options.ArrayFilters.Concat(new List <ArrayFilterDefinition> {
                def
            });

            return(this);
        }
        private static void Main(string[] args)
        {
            new DB("test", "localhost");
            // create seed data
            var seed = new Disclaimer
            {
                Overrides = new[]
                {
                    new Override
                    {
                        CategoryId = 666, EffectiveDate = DateTime.Now.AddDays(-1), Message = "disclaimer 1"
                    },
                    new Override
                    {
                        CategoryId = 666, EffectiveDate = DateTime.Now.AddDays(-1), Message = "disclaimer 2"
                    },
                    new Override
                    {
                        CategoryId = 777, EffectiveDate = DateTime.Now.AddDays(-1), Message = "disclaimer 3"
                    }
                }
            }; seed.Save();
            // defind array filter to target only 666
            ArrayFilterDefinition <Disclaimer> arrFilter = "{ 'x.CategoryId' : 666 }";

            // start bulk update command
            DB.Update <Disclaimer>()
            // step1: set expiry date on existing 666s
            .Match(d => d.ID == seed.ID)
            .Option(o => o.ArrayFilters = new[] { arrFilter })
            .Modify(b => b.Set("Overrides.$[x].ExpiryDate", DateTime.Now))
            .AddToQueue()
            // step2: add a new 666
            .Match(d => d.ID == seed.ID)
            .Modify(b => b.Push(d => d.Overrides,
                                new Override
            {
                CategoryId    = 666,
                EffectiveDate = DateTime.Now,
                Message       = "disclaimer 4"
            }))
            .AddToQueue()
            // run two step bulk update command
            .Execute();
        }
Exemple #4
0
        public JObject UpdateMany(string collection, string updateQuery, string filter, params string[] arrayFilters)
        {
            FilterDefinition <BsonDocument> filterDoc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize <BsonDocument>(filter);
            UpdateDefinition <BsonDocument> update    = MongoDB.Bson.Serialization.BsonSerializer.Deserialize <BsonDocument>(updateQuery);
            var arrFilters = new List <ArrayFilterDefinition>();

            foreach (var f in arrayFilters)
            {
                ArrayFilterDefinition <BsonDocument> arrayFtr = MongoDB.Bson.Serialization.BsonSerializer.Deserialize <BsonDocument>(f);
                arrFilters.Add(arrayFtr);
            }

            var result = _mongoDataBase.GetCollection <BsonDocument>(collection).UpdateMany(filterDoc, update, new UpdateOptions {
                ArrayFilters = arrFilters
            });

            return(JObject.Parse(result.ToJson()));
        }