static ProductRepo() { Collection = DatabaseFactory.CreateMongoDatabase().GetCollection <Product>("products"); }
static void Main(string[] args) { var sw = new Stopwatch(); sw.Start(); var from = DatabaseFactory.CreateMongoDatabase(); var to = DatabaseFactory.CreateMongoDatabase("light-1.com"); if (false) { var categoryCollection = to.GetCollection <Category>("categories"); categoryCollection.Drop(); categoryCollection.EnsureIndex(IndexKeys.Ascending("Source", "Number"), IndexOptions.SetUnique(true)); var categories = from.GetCollection <Category>("categories").FindAll(); categoryCollection.InsertBatch(categories); } var productCollection = to.GetCollection <Product>("products"); productCollection.Drop(); var products = from.GetCollection <Product>("products").FindAll().ToList(); var productsCollection = new List <List <Product> >(); const int BATCH_SIZE = 200; for (var offset = 0; offset < products.Count; offset += BATCH_SIZE) { Console.Write("."); var productsInBatch = products.Skip(offset).Take(BATCH_SIZE).ToList(); productsCollection.Add(productsInBatch); } var failedProducts = new ConcurrentBag <Product>(); Parallel.ForEach(productsCollection, //new ParallelOptions { MaxDegreeOfParallelism = 4 }, productsInBatch => { try { Console.Write("+"); productCollection.InsertBatch(productsInBatch); } catch { Console.Write("x"); foreach (var p in productsInBatch) { failedProducts.Add(p); } } }); for (var offset = 0; offset < failedProducts.Count; offset += BATCH_SIZE) { Console.Write("="); var productsInBatch = failedProducts.Skip(offset).Take(BATCH_SIZE).ToList(); productCollection.InsertBatch(productsInBatch); } productCollection.EnsureIndex(IndexKeys.Ascending("Source", "Number"), IndexOptions.SetUnique(true)); }
/* * /// <summary> * /// Gets the database from connection string. * /// </summary> * /// <param name="connectionString">The connection string.</param> * /// <returns>MongoDatabase.</returns> * /// <exception cref="System.Exception">No database name specified in connection string</exception> * private MongoDatabase GetDatabaseFromSqlStyle(string connectionString) * { * var conString = new MongoConnectionStringBuilder(connectionString); * MongoClientSettings settings = MongoClientSettings.FromConnectionStringBuilder(conString); * MongoServer server = new MongoClient(settings).GetServer(); * if (conString.DatabaseName == null) * { * throw new Exception("No database name specified in connection string"); * } * return server.GetDatabase(conString.DatabaseName); * } * * /// <summary> * /// Gets the database from URL. * /// </summary> * /// <param name="url">The URL.</param> * /// <returns>MongoDatabase.</returns> * private MongoDatabase GetDatabaseFromUrl(MongoUrl url) * { * var client = new MongoClient(url); * MongoServer server = client.GetServer(); * if (url.DatabaseName == null) * { * throw new Exception("No database name specified in connection string"); * } * return server.GetDatabase(url.DatabaseName); // WriteConcern defaulted to Acknowledged * } * * /// <summary> * /// Uses connectionString to connect to server and then uses databae name specified. * /// </summary> * /// <param name="connectionString">The connection string.</param> * /// <param name="dbName">Name of the database.</param> * /// <returns>MongoDatabase.</returns> * private MongoDatabase GetDatabase(string connectionString, string dbName) * { * var client = new MongoClient(connectionString); * MongoServer server = client.GetServer(); * return server.GetDatabase(dbName); * } */ #endregion #region Constructors /// <summary> /// Initializes a new instance of the <see cref="UserStore{TUser}" /> class. Uses DefaultConnection name if none was /// specified. /// </summary> public UserStore() //: this("SkillSmart") { db = DatabaseFactory.CreateMongoDatabase(); }
static RatioRankingRepo() { Collection = DatabaseFactory.CreateMongoDatabase().GetCollection <RatioRanking>("ratio_rankings"); }
static CategoryRepo() { Collection = DatabaseFactory.CreateMongoDatabase().GetCollection <Category>("categories"); }
static PriceHistoryRepo() { Collection = DatabaseFactory.CreateMongoDatabase().GetCollection <PriceHistory>("price_history"); }