Exemplo n.º 1
0
        public static void GetTraincars(train t, IMongoDatabase db)
        {
            var tcars  = db.GetCollection <trainCar>("trainCars");
            var trains = db.GetCollection <train>("trains");
            var pp     = from train in trains.AsQueryable()
                         where train.Id == t.Id
                         join tc in tcars on train.Id equals tc.trainId into joined
                         from sub_o in joined.DefaultIfEmpty()
                         select sub_o;

            t.trainCars = pp.ToList <trainCar>();
        }
Exemplo n.º 2
0
        private static void Main(string[] args)
        {
            var server   = new MongoClient("mongodb://localhost");
            var database = server.GetDatabase("testmongodb");

            foreach (var collectionName in database.ListCollections().ToList())
            {
                database.DropCollection(collectionName[0].AsString);
            }

            var collection       = database.GetCollection <trainCar>("trainCars");
            var trainCollection  = database.GetCollection <train>("trains");
            var peopleCollection = database.GetCollection <passenger>("passengers");


            //add some data
            for (int t = 0; t < 20; t++)
            {
                train newTrain = new train();
                newTrain.note      = "nyc" + t.ToString();
                newTrain.trainNo   = "345";
                newTrain.trainCars = new List <trainCar>();
                trainCollection.InsertOne(newTrain);


                //add some cars:

                for (int i = 0; i < 50; i++)
                {
                    trainCar tcar = new trainCar();

                    tcar.carNo      = "0" + i.ToString();
                    tcar.note       = "Needs new brakes";
                    tcar.serial     = "1234";
                    tcar.trainId    = newTrain.Id;
                    tcar.passengers = new List <passenger>();
                    collection.InsertOne(tcar);

                    newTrain.trainCars.Add(tcar);

                    for (int p = 0; p < 3; p++)
                    {
                        passenger pass = new passenger();
                        pass.trainCarId = tcar.Id;
                        pass.Name       = "name" + p.ToString();
                        peopleCollection.InsertOne(pass);
                        tcar.passengers.Add(pass);
                    }
                }
            }
            Console.WriteLine("saved");


            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            //get the data
            //asparallel gained me about 15% of qry time
            var query = from v in trainCollection.AsQueryable <train>().AsParallel()
                        select v;

            foreach (train aTrain in query)
            {
                GetTraincars(aTrain, database);
                Console.WriteLine("train: " + aTrain.note);
                foreach (trainCar tcar2 in aTrain.trainCars)
                {
                    if (tcar2 != null)
                    {
                        Console.WriteLine("car: " + tcar2.carNo);
                        GetPassengers(tcar2, database);
                        foreach (passenger pass in tcar2.passengers)
                        {
                            Console.WriteLine("car: " + tcar2.carNo + " pass: "******"trains loaded");

            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;

            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                               ts.Hours, ts.Minutes, ts.Seconds,
                                               ts.Milliseconds / 10);

            Console.WriteLine("RunTime " + elapsedTime);

            Console.ReadLine();
        }