Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // get a mongoclient using the default connection string
            var mongo = new MongoClient("mongodb://*****:*****@localhost/UserPositionsDb");

            // get (and create if doesn't exist) a database from the mongoclient
            var db = mongo.GetDatabase("UserPositionsDb");

            // get a collection of User (and create if it doesn't exist)
            var collection = db.GetCollection <User>("UserCollection");

            // Random positions generator
            Random rand = new Random(DateTime.Now.Second);

            Stopwatch sw = new Stopwatch();

            bool upsert = true;

            // Increase the day number to simulate the daily update for many users
            // to check how the record addition slows the db when the records number grows
            for (int day = 0; day < 100; day++)
            {
                List <WriteModel <User> > _listBulkUserWrites = new List <WriteModel <User> >();

                sw.Reset();
                sw.Start();

                // Loop for 16*12 users data update
                for (int userID = 1000; userID < 1000 + 16 * 12; userID++)
                {
                    // Add the new record to the document
                    var positionItem = new PositionItem
                    {
                        Position     = (41 + rand.NextDouble() * 2).ToString() + "," + (11 + rand.NextDouble() * 2).ToString(),
                        PositionDate = day
                    };

                    var updateUserFilter = Builders <User> .Filter.Eq(u => u.UserId, userID);

                    var updateUserDefinition = Builders <User> .Update.Push(u => u.UserPositions, positionItem);

                    _listBulkUserWrites.Add(new UpdateOneModel <User>(updateUserFilter,
                                                                      updateUserDefinition)
                    {
                        IsUpsert = upsert
                    }

                                            );
                }

                if (_listBulkUserWrites.Count > 0)
                {
                    collection.BulkWriteAsync(_listBulkUserWrites);
                }

                sw.Stop();

                Console.WriteLine("Elapsed Time = {0}ms", sw.Elapsed.TotalMilliseconds);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter User ID:");
            // Get ID from console
            var userIDStr = Console.ReadLine();

            Console.WriteLine("Enter Day:");
            // Get data from console
            var dataDayStr = Console.ReadLine();

            Console.WriteLine("Enter Position:");
            // Get some input from user
            var dataPosStr = Console.ReadLine();

            if (!int.TryParse(userIDStr, out int userID))
            {
                Console.WriteLine("Error reading user ID");
                return;
            }
            if (!int.TryParse(dataDayStr, out int positionDay))
            {
                Console.WriteLine("Error reading position day");
                return;
            }

            // Retrieve actual
            var positionItem = new PositionItem()
            {
                PositionDate = positionDay,
                Position     = dataPosStr
            };

            // get a mongoclient using the default connection string
            var mongo = new MongoClient("mongodb://*****:*****@localhost/UserPositionsDb");

            // get (and create if doesn't exist) a database from the mongoclient
            var db = mongo.GetDatabase("UserPositionsDb");

            // get a collection of User (and create if it doesn't exist)
            var collection = db.GetCollection <User>("UserCollection");

            // Instead of doing 2 round trips (first retrieving and then updating), the Update instruction
            // coupled to the push operator can be used to make the insert instruction more efficient
            var updatePositionFilter = Builders <User> .Update.Push(u => u.UserPositions, positionItem);

            collection.UpdateOne(u => u.UserId == userID,
                                 updatePositionFilter,
                                 new UpdateOptions {
                IsUpsert = true
            });
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter User ID:");
            // Get ID from console
            var userIDStr = Console.ReadLine();

            Console.WriteLine("Enter Day:");
            // Get data from console
            var dataDayStr = Console.ReadLine();

            Console.WriteLine("Enter Position:");
            // Get some input from user
            var dataPosStr = Console.ReadLine();

            if (!int.TryParse(userIDStr, out int userID))
            {
                Console.WriteLine("Error reading user ID");
                return;
            }
            if (!int.TryParse(dataDayStr, out int positionDay))
            {
                Console.WriteLine("Error reading position day");
                return;
            }

            // Retrieve actual
            var positionItem = new PositionItem()
            {
                PositionDate = positionDay,
                Position     = dataPosStr
            };

            // get a mongoclient using the connection string
            var mongo = new MongoClient("mongodb://*****:*****@localhost/UserPositionsDb");

            // get (and create if doesn't exist) a database from the mongoclient
            var db = mongo.GetDatabase("FmpMongoDb");

            // get a collection of User (and create if it doesn't exist)
            var collection = db.GetCollection <User>("UserCollection");

            var user = collection.AsQueryable()
                       .SingleOrDefault(p => p.UserId == userID);

            // If the user does not exist, create a new one
            bool newUser = false;

            if (user == null)
            {
                user = new User
                {
                    UserId        = userID,
                    UserPositions = new List <PositionItem>()
                };
                newUser = true;
            }

            Console.WriteLine($"User before the item addition");
            Console.Write(user);

            user.UserPositions.Add(positionItem);

            Console.WriteLine($"User after the item addition");
            Console.Write(user);

            // Add the entered item to the collection
            if (newUser)
            {
                collection.InsertOne(user);
            }
            else
            {
                collection.ReplaceOne(u => u.UserId == userID, user);
            }

            // Count the items in the collection post insert
            var count = user.UserPositions.Count();

            Console.WriteLine($"Number of items in the collection after insert: {count}");
        }