コード例 #1
0
        public static void EnsureUniqueIndexOnRoleName(MongoCollection roles)
        {
            var roleName = new IndexKeysBuilder <IdentityRole>().Ascending(t => t.Name);
            var unique   = new IndexOptionsBuilder().SetUnique(true);

            roles.CreateIndex(roleName, unique);
        }
コード例 #2
0
        public static void EnsureUniqueIndexOnEmail(MongoCollection users)
        {
            var email  = new IndexKeysBuilder <IdentityUser>().Ascending(t => t.Email);
            var unique = new IndexOptionsBuilder().SetUnique(true);

            users.CreateIndex(email, unique);
        }
コード例 #3
0
        public static void InsertErpOrderPartner(string orderId, string parNr, MongoDatabase mongoDatabase, ILogger logger)
        {
            string collName = "erpOrderPartner";

            if (!mongoDatabase.CollectionExists(collName))
            {
                logger.LogDebug("Collection doesn't exist. Creating it.", collName);

                mongoDatabase.CreateCollection(collName);
                MongoCollection <BsonDocument> newErpEntriesCollection = mongoDatabase.GetCollection(collName);
                IndexKeysBuilder Key = IndexKeys.Ascending("orderid");
                newErpEntriesCollection.CreateIndex(Key);
            }
            MongoCollection <BsonDocument> erpEntriesCollection = mongoDatabase.GetCollection(collName);
            MongoCursor cursor = erpEntriesCollection.Find(Query.And(Query.EQ("orderid", orderId), Query.EQ("parnr", parNr)));

            if (cursor.Count() == 0)
            {
                BsonDocument orderPartnerDocument = new BsonDocument();
                ObjectId     currentProcess_id    = ObjectId.GenerateNewId();
                orderPartnerDocument.Set(DBQuery.Id, currentProcess_id);
                orderPartnerDocument.Set("orderid", orderId);
                orderPartnerDocument.Set("parnr", parNr);
                orderPartnerDocument.Set("created", DateTime.UtcNow);
                erpEntriesCollection.Save(orderPartnerDocument, WriteConcern.Acknowledged);
            }
        }
コード例 #4
0
        /// <summary>
        ///     添加索引
        /// </summary>
        /// <param name="IdxOpt"></param>
        /// <param name="option"></param>
        /// <param name="currentCollection"></param>
        /// <returns></returns>
        public static bool CreateMongoIndex(IndexOption IdxOpt,
                                            IndexOptionsBuilder option, MongoCollection currentCollection, ref string errorMessage)
        {
            var mongoCol  = currentCollection;
            var indexkeys = new IndexKeysBuilder();

            if (!string.IsNullOrEmpty(IdxOpt.GeoSpatialHaystackKey))
            {
                indexkeys.GeoSpatialHaystack(IdxOpt.GeoSpatialHaystackKey);
            }
            if (!string.IsNullOrEmpty(IdxOpt.GeoSpatialKey))
            {
                indexkeys.GeoSpatial(IdxOpt.GeoSpatialKey);
            }
            if (!string.IsNullOrEmpty(IdxOpt.GeoSpatialSphericalKey))
            {
                indexkeys.GeoSpatialSpherical(IdxOpt.GeoSpatialSphericalKey);
            }
            indexkeys.Ascending(IdxOpt.AscendingKey.ToArray());
            indexkeys.Descending(IdxOpt.DescendingKey.ToArray());
            indexkeys.Text(IdxOpt.TextKey.ToArray());
            //CreateIndex失败的时候会出现异常!
            try
            {
                var result = mongoCol.CreateIndex(indexkeys, option);
                return(result.Response.GetElement("ok").Value.AsInt32 == 1);
            }
            catch (Exception ex)
            {
                errorMessage = ex.ToString();
                return(false);
            }
        }
コード例 #5
0
        /// <summary>
        /// Initializes all the needed indexes for the pages to optimize search on them.
        /// </summary>
        public void InitializeIndexes()
        {
            // key options
            var options = new IndexOptionsBuilder().SetUnique(true);

            // page search key
            var keys = new IndexKeysBuilder().Ascending("Spot");

            Spots.EnsureIndex(keys, options);

            // general page search
            keys = new IndexKeysBuilder().Ascending("_id");
            Spots.EnsureIndex(keys, options);

            options.SetUnique(false);

            keys = new IndexKeysBuilder().Ascending("ParcelId");
            Spots.EnsureIndex(keys, options);

            keys = new IndexKeysBuilder().Ascending("CustomerId");
            Spots.EnsureIndex(keys, options);

            keys = new IndexKeysBuilder().Ascending("SponsorId");
            Spots.EnsureIndex(keys, options);
        }
コード例 #6
0
        /// <summary>
        /// Verify all required <see cref="IMongoIndexKeys"/> are defined and ready to go.
        /// </summary>
        protected virtual void VerifyIndexes <TEntity>(MongoCollection <TEntity> collection)
        {
            Type entityType = typeof(TEntity);

            if (IndexTypesByEntityType.ContainsKey(entityType))
            {
                foreach (object untypedIndexType in IndexTypesByEntityType[entityType])
                {
                    var mongoIndex = (MongoIndex <TEntity>)untypedIndexType;

                    var indexKeysBuilder = new IndexKeysBuilder();
                    if (mongoIndex.IsAcending)
                    {
                        indexKeysBuilder = indexKeysBuilder.Ascending(mongoIndex.Selectors.ToArray());
                    }
                    else
                    {
                        indexKeysBuilder = indexKeysBuilder.Descending(mongoIndex.Selectors.ToArray());
                    }

                    collection.CreateIndex
                    (
                        indexKeysBuilder,
                        IndexOptions
                        .SetUnique(mongoIndex.IsUnique)
                        .SetName(mongoIndex.Name)
                    );
                }
            }
        }
コード例 #7
0
        public void CreateIndex <T>(Tuple <string, bool>[] names, bool background = true, TimeSpan ttl = new TimeSpan(), bool unique = false, bool dropDups = false, bool sparse = false)
        {
            var ascending  = new List <string>();
            var descending = new List <string>();

            foreach (var n in names)
            {
                if (n.Item2)
                {
                    ascending.Add(n.Item1);
                }
                else
                {
                    descending.Add(n.Item1);
                }
            }

            var builder = new IndexKeysBuilder();

            if (ascending.Count > 0)
            {
                builder = builder.Ascending(ascending.ToArray());
            }
            if (descending.Count > 0)
            {
                builder = builder.Descending(descending.ToArray());
            }

            _database.GetCollection <T>(typeof(T).Name).EnsureIndex(builder,
                                                                    IndexOptions.SetBackground(background)
                                                                    .SetTimeToLive(ttl)
                                                                    .SetUnique(unique)
                                                                    .SetDropDups(dropDups)
                                                                    .SetSparse(sparse));
        }
コード例 #8
0
        public static void EnsureUniqueIndexOnUserName(MongoCollection users)
        {
            var userName = new IndexKeysBuilder <IdentityUser>().Ascending(t => t.UserName);
            var unique   = new IndexOptionsBuilder().SetUnique(true);

            users.CreateIndex(userName, unique);
        }
コード例 #9
0
        private void Init()
        {
            IndexKeysBuilder    keys        = IndexKeys.Ascending("metadata.directory_name");
            IndexOptionsBuilder options     = IndexOptions.SetName("directory_name").SetBackground(false);
            MongoGridFS         mongoGridFs = GetGridFS();

            mongoGridFs.EnsureIndexes();
            mongoGridFs.Files.EnsureIndex(keys, options);
        }
コード例 #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_columns"></param>
        /// <returns></returns>
        public static IMongoIndexKeys GetIndexKeys(IEnumerable <string> _columns)
        {
            string[]         columns = _columns.ToArray();
            IndexKeysBuilder ikb     = new IndexKeysBuilder();

            ikb.Ascending(columns);

            return(ikb);
        }
コード例 #11
0
        public static void BuildGeospatialIndex(MongoCollection _table,
                                                string _column,
                                                string _indexName)
        {
            IndexKeysBuilder   ikb     = IndexKeys.GeoSpatialSpherical(_column);
            IMongoIndexOptions options = GetIndexOptions(_indexName, false);

            _table.CreateIndex(ikb, options);
        }
コード例 #12
0
        /// <summary>
        /// 添加索引
        /// </summary>
        /// <param name="AscendingKey"></param>
        /// <param name="DescendingKey"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static Boolean CreateMongoIndex(String[] AscendingKey, String[] DescendingKey, IndexOptionsBuilder option)
        {
            MongoCollection  mongoCol  = SystemManager.GetCurrentCollection();
            IndexKeysBuilder indexkeys = new IndexKeysBuilder();

            indexkeys.Ascending(AscendingKey);
            indexkeys.Descending(DescendingKey);
            mongoCol.CreateIndex(indexkeys, option);
            return(true);
        }
コード例 #13
0
 private void SafeDropIndex(IndexKeysBuilder <Person> indexKeysBuilder)
 {
     try
     {
         mongo.PeopleCollection.DropIndex(indexKeysBuilder);
     }
     catch
     {
     }
 }
コード例 #14
0
 public MongoIndexKeysWarpper Text(params string[] names)
 {
     if (MongoIndexKeys == null)
     {
         MongoIndexKeys = IndexKeys.Text(names);
     }
     else
     {
         MongoIndexKeys = MongoIndexKeys.Text(names);
     }
     return(this);
 }
コード例 #15
0
        private void IndexExamples()
        {
            //insert
            IndexKeysBuilder <Car> carIndexBuilder = IndexKeys <Car> .Ascending(c => c.Make, c => c.NumberOfDoors);

            IndexOptionsBuilder <Car> carIndexOptions = IndexOptions <Car> .SetName("Car_CompositeIndex").SetTimeToLive(new TimeSpan(2, 0, 0, 0));

            CarRentalContext.Cars.EnsureIndex(carIndexBuilder, carIndexOptions);

            //delete
            CarRentalContext.Cars.DropIndexByName("Car_CompositeIndex");
        }
コード例 #16
0
 public MongoIndexKeysWarpper GeoSpatial(string name)
 {
     if (MongoIndexKeys == null)
     {
         MongoIndexKeys = IndexKeys.GeoSpatial(name);
     }
     else
     {
         MongoIndexKeys = MongoIndexKeys.GeoSpatial(name);
     }
     return(this);
 }
コード例 #17
0
 public MongoIndexKeysWarpper TextAll()
 {
     if (MongoIndexKeys == null)
     {
         MongoIndexKeys = IndexKeys.TextAll();
     }
     else
     {
         MongoIndexKeys = MongoIndexKeys.TextAll();
     }
     return(this);
 }
コード例 #18
0
 public MongoIndexKeysWarpper GeoSpatialHaystack(string name, string additionalName)
 {
     if (MongoIndexKeys == null)
     {
         MongoIndexKeys = IndexKeys.GeoSpatialHaystack(name, additionalName);
     }
     else
     {
         MongoIndexKeys = MongoIndexKeys.GeoSpatialHaystack(name, additionalName);
     }
     return(this);
 }
コード例 #19
0
        protected override void OK()
        {
            var b = new IndexKeysBuilder();

            b.Ascending(SelectedField);
            var b1 = new IndexOptionsBuilder();

            b1.SetName(IndexName);
            _coll.CreateIndex(b, b1);

            ExistsIndexes = new ObservableCollection <IndexInfo>(GetExistsIndexes());
        }
コード例 #20
0
        public MongoIndexKeysWarpper Descending(params string[] names)
        {
            if (MongoIndexKeys == null)
            {
                MongoIndexKeys = IndexKeys.Descending(names);
            }
            else
            {
                MongoIndexKeys = MongoIndexKeys.Descending(names);
            }

            return(this);
        }
コード例 #21
0
        public MongoIndexKeysWarpper Hashed(string name)
        {
            if (MongoIndexKeys == null)
            {
                MongoIndexKeys = IndexKeys.Hashed(name);
            }
            else
            {
                MongoIndexKeys = MongoIndexKeys.Hashed(name);
            }

            return(this);
        }
コード例 #22
0
        /// <summary>
        /// 添加索引
        /// </summary>
        /// <param name="AscendingKey"></param>
        /// <param name="DescendingKey"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static Boolean CreateMongoIndex(String[] AscendingKey, String[] DescendingKey, String GeoSpatialKey, IndexOptionsBuilder option)
        {
            MongoCollection  mongoCol  = SystemManager.GetCurrentCollection();
            IndexKeysBuilder indexkeys = new IndexKeysBuilder();

            if (!String.IsNullOrEmpty(GeoSpatialKey))
            {
                indexkeys.GeoSpatial(GeoSpatialKey);
            }
            indexkeys.Ascending(AscendingKey);
            indexkeys.Descending(DescendingKey);
            mongoCol.EnsureIndex(indexkeys, option);
            return(true);
        }
コード例 #23
0
        public void EnsureIndex(Dictionary <string, object> matchData)
        {
            // matchKeys, Interval, Left
            IndexKeysBuilder ikb = new IndexKeysBuilder();

            ikb = ikb.Ascending(matchData.Keys.ToArray());

            CheckConnection();
            Collection.EnsureIndex(ikb);
            //
            var indexE = IndexKeys <MongoRange> .Ascending(r => r.SecondsInterval).Ascending(r => r.Left);

            Collection.EnsureIndex(indexE);
        }
コード例 #24
0
        /// <summary>
        /// Initializes all the needed indexes for the pages to optimize search on them.
        /// </summary>
        public void InitializeIndexes()
        {
            // key options
            var options = new IndexOptionsBuilder().SetUnique(true);

            // page search key
            var keys = new IndexKeysBuilder().Ascending("Role");

            Roles.EnsureIndex(keys, options);

            // general page search
            keys = new IndexKeysBuilder().Ascending("_id");
            Roles.EnsureIndex(keys, options);
        }
コード例 #25
0
        private void CreateChangeRequestIndexes()
        {
            var keys = new IndexKeysBuilder();

            keys.Ascending("Id");

            var options = new IndexOptionsBuilder();

            options.SetSparse(true);
            options.SetUnique(true);

            var collection = database.GetCollection <CardChange>("card_changes");

            collection.EnsureIndex(keys, options);
        }
コード例 #26
0
        public void insert()
        {
            bool b = db._mongoCollection.Database.CollectionExists("MemberIntegralLock");

            this.created = DateTime.Now;
            db.Insert(this);
            if (!b)
            {
                IndexKeysBuilder keys = new IndexKeysBuilder();
                keys.Descending("created");
                keys.Ascending("uid");
                keys.Ascending("mte");
                db._mongoCollection.CreateIndex(keys);
            }
        }
コード例 #27
0
        private void CreateUserCardIndexes()
        {
            var keys = new IndexKeysBuilder();

            keys.Ascending("PlaneswalkerId", "MultiverseId");

            var options = new IndexOptionsBuilder();

            options.SetSparse(true);
            options.SetUnique(true);

            var collection = database.GetCollection <UserCard>("user_cards");

            collection.EnsureIndex(keys, options);
        }
コード例 #28
0
        /// <summary>
        ///     添加索引
        /// </summary>
        /// <param name="ascendingKey"></param>
        /// <param name="descendingKey"></param>
        /// <param name="geoSpatialKey"></param>
        /// <param name="option"></param>
        /// <param name="currentCollection"></param>
        /// <returns></returns>
        public static bool CreateMongoIndex(string[] ascendingKey, string[] descendingKey, string geoSpatialKey,
                                            IndexOptionsBuilder option, MongoCollection currentCollection)
        {
            var mongoCol  = currentCollection;
            var indexkeys = new IndexKeysBuilder();

            if (!string.IsNullOrEmpty(geoSpatialKey))
            {
                indexkeys.GeoSpatial(geoSpatialKey);
            }
            indexkeys.Ascending(ascendingKey);
            indexkeys.Descending(descendingKey);
            mongoCol.CreateIndex(indexkeys, option);
            return(true);
        }
コード例 #29
0
        private void CreateRoleIndexes()
        {
            var keys = new IndexKeysBuilder();

            keys.Ascending("Name", "DomainId");

            var options = new IndexOptionsBuilder();

            options.SetSparse(true);
            options.SetUnique(true);

            var collection = _database.GetCollection <Role> ("roles");

            collection.EnsureIndex(keys, options);
        }
コード例 #30
0
        private void CreateAdminIndexes()
        {
            var keys = new IndexKeysBuilder();

            keys.Ascending("DomainId", "ManagerId");

            var options = new IndexOptionsBuilder();

            options.SetSparse(true);
            options.SetUnique(true);

            var collection = _database.GetCollection <Administrator> ("administrators");

            collection.EnsureIndex(keys, options);
        }
コード例 #31
0
 public static void EnsureUniqueIndexOnEmail(MongoCollection users)
 {
     var email = new IndexKeysBuilder<IdentityUser>().Ascending(t => t.Email);
     var unique = new IndexOptionsBuilder().SetUnique(true);
     users.CreateIndex(email, unique);
 }
コード例 #32
0
 public static void EnsureUniqueIndexOnUserName(MongoCollection users)
 {
     var userName = new IndexKeysBuilder<IdentityUser>().Ascending(t => t.UserName);
     var unique = new IndexOptionsBuilder().SetUnique(true);
     users.CreateIndex(userName, unique);
 }
コード例 #33
0
 public static void EnsureUniqueIndexOnRoleName(MongoCollection roles)
 {
     var roleName = new IndexKeysBuilder<IdentityRole>().Ascending(t => t.Name);
     var unique = new IndexOptionsBuilder().SetUnique(true);
     roles.CreateIndex(roleName, unique);
 }