/// <summary>
        /// Create a Nut for a specific application account and in a specific configuration container.
        /// </summary>
        /// <param name="account">The application account we are creating the nut for.</param>
        /// <param name="container">The container we are inserting the nut into (e.g. connectionstrings, endpoints, appconfig, etc).</param>
        /// <param name="nut">Busta nut.</param>
        /// <returns>True if the nut was added and false if it already exists, and therefore was not added/updated.</returns>
        public static bool CreateNut(string account, string container, Nut nut)
        {
            bool nutAdded = false;

            Mongo mongo = new Mongo();
            mongo.Connect();

            var db = mongo.GetDatabase(WellKnownDb.AppConfiguration);

            var collection = db.GetCollection(container);

            var doc = new Document();

            doc["account"] = account;
            doc["name"] = nut.Key;
            doc["value"] = nut.Value;

            if (nut.Properties != null)
            {
                foreach (var k in nut.Properties.Keys)
                {
                    doc.Add(k, nut.Properties[k]);
                }
            }

            if (collection.FindOne(doc) == null)
            {
                collection.Insert(doc);
                nutAdded = true;
            }

            return nutAdded;
        }
Esempio n. 2
0
        public void FavoriteUpdate_Multiple()
        {
            string connstr = ConfigurationManager.AppSettings["mongoDBTest"];
            var mongo = new Mongo(connstr);
            mongo.Connect();

            var testDB = mongo.GetDatabase("myTest");
            UserFavorite fav = new UserFavorite(testDB, "lynn");

            fav.Update("q", 1, "t1,t2,t2");
        }
        public static List<Nut> FindAllInContainer(string account, string container, IDictionary<string,string> properties)
        {
            List<Nut> nuts = new List<Nut>();

            Mongo mongo = new Mongo();
            mongo.Connect();

            var db = mongo.GetDatabase(WellKnownDb.AppConfiguration);

            var collection = db.GetCollection(container);

            var query = new Document();

            query["account"] = account;

            foreach (string k in properties.Keys)
            {

                var t = properties[k];

                if (k == "value")
                {
                    query["value"] = t as string;
                }
                else
                {
                    query[k] = properties[k];

                }
            }

            var result = collection.Find(query);

            foreach (var r in result.Documents)
            {

                var nut = new Nut { Database = WellKnownDb.AppConfiguration, Table = container, Key = r["name"] as string, Properties = new Dictionary<string, string>() };

                nut.Value = r["value"] as string;
                nut.Uri = string.Format("/{0}/{1}/{2}", account, container, r["name"]);

                foreach (var j in r.Keys)
                {
                    nut.Properties.Add(j, r[j] as string);
                }

                nuts.Add(nut);
            }

            return nuts;
        }
        public void TestSetAndGetFunction()
        {
            string connstr = ConfigurationManager.AppSettings["mongoDBTest"];
            var mongo = new Mongo(connstr);
            mongo.Connect();

            var testDB = mongo.GetDatabase("myTest");

            AdminParameters.DB = testDB;

            AdminParameters.Set(AdminParameters.SuggesttedTagsForUserFavorite, "hello");
            Assert.AreEqual(AdminParameters.Get(AdminParameters.SuggesttedTagsForUserFavorite), "hello");

            mongo.Disconnect();
        }
Esempio n. 5
0
        public void FavoriteAdd()
        {
            string connstr = ConfigurationManager.AppSettings["mongoDBTest"];
            var mongo = new Mongo(connstr);
            mongo.Connect();

            var testDB = mongo.GetDatabase("myTest");
            UserFavorite fav = new UserFavorite(testDB, "testUserName" + DateTime.Now.ToShortTimeString());

            fav.Update("question", 101, "tag1,tag2");
            fav.Update("question", 102, "tag3,tag4");
            fav.Update("question", 104, "tag3,tag4,tag5");
            fav.Update("question", 103, "tag1,tag2");
            fav.Update("k", 101, "tag1,tag2");
            fav.Update("k", 102, "tag2,tag7");
            fav.Update("k", 103, "tag1,tag5");
            fav.Update("k", 104, "tag1,tag5");
            fav.Update("k", 105, "tag1,tag5");
            fav.Update("k", 106, "tag1,tag5");

            Assert.IsTrue(Compare(fav.GetTags("k"), "tag1,tag2,tag7,tag5"));
            Assert.IsTrue(Compare(fav.GetTags("question"), "tag1,tag2,tag3,tag4,tag5"));
            Assert.AreEqual("tag1,tag5", fav.GetTags("k", 103));
            Assert.AreEqual("tag1,tag2", fav.GetTags("question", 103));

            Assert.IsTrue(fav.GetIDs("question", "tag1").Contains(101));
            Assert.IsTrue(fav.GetIDs("question", "tag1").Contains(103));

            Assert.AreEqual(5, fav.GetIDs("k", "tag1").Count);
            Assert.AreEqual(6, fav.GetIDs("k").Count);
            Assert.AreEqual(0, fav.GetIDs("foofoo").Count);
            Assert.AreEqual(4, fav.GetIDs("question").Count);

            Assert.IsTrue(fav.HasFavorite("question", 103));
            Assert.IsFalse(fav.HasFavorite("k", 203));

            fav.Remove("question", 103);

            Assert.IsFalse(fav.HasFavorite("question", 103));

            mongo.Disconnect();
        }
Esempio n. 6
0
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">The args.</param>
        private static void Main( string[] args )
        {
            // Connect to the mongo instance.
            var mongo = new Mongo();
            mongo.Connect();

            // Use the myorders database.
            Database db = mongo.GetDatabase("myorders");

            // Get the orders collection.
            IMongoCollection orders = db.GetCollection("orders");

            DeleteAllDocumentsFromCollection(orders);
            CreateAndInsertDocumentIntoCollection(orders);

            QueryFromCollection( orders );

            Console.WriteLine("\nPress Enter to Exit.");
            Console.ReadLine();
        }
        protected void CreateMongoDB(String dbName)
        {
            //mongod.exe must be running for this to work.
            var mongo = new Mongo();
            mongo.Connect();

            //if the database is not found in c:\data\db it will be created.
            var db = mongo.GetDatabase(dbName);

            //declare a new "table"
            var categories = db.GetCollection("categories");

            //create a new key value set
            var document = new Document();
            document["Name"] = "Product";
            document["Name"] = "Price";

            //create the "tabt"
            categories.Insert(document);
            mongo.Disconnect();
        }
        protected String GetMongoDBResults(String dbName)
        {
            //mongod.exe must be running for this to work.
            var mongo = new Mongo();
            mongo.Connect();

            //if the database is not found in c:\data\db it will be created.
            var db = mongo.GetDatabase(dbName);

            //declare a new "table"
            var categories = db.GetCollection("categories");

            //get the categories table
            var category = categories.FindOne(new Document() { { "Name","Price" } });
            //alternatively you can get all documents in the database
            var documents = categories.FindAll().Documents;
            String res = category["Name"].ToString();

            //diconnect
            mongo.Disconnect();

            //return results in a EF friendly shapre just because that's what I'm working with mostly nowadays
            return res;
        }
        // database
        private void ConnectToDatabase()
        {
            db_server = new MongoDB.Driver.Mongo(_connectionString);

            db_server.Connect();

            MongoDB.Driver.IMongoDatabase db = db_server.GetDatabase("MongoBlog");

            collection = db.GetCollection("blogposts");

            return;
        }
Esempio n. 10
0
        public static Nut FindNut(string account, string container, string name, IDictionary<string,string> properties)
        {
            Nut nut = null;

            Mongo mongo = new Mongo();
            mongo.Connect();

            var db = mongo.GetDatabase(WellKnownDb.AppConfiguration);

            var collection = db.GetCollection(container);

            var query = new Document();

            query["name"] = name;
            query["account"] = account;

            foreach (string k in properties.Keys)
            {
                query[k] = properties[k];
            }

            var result = collection.FindOne(query);

            if (result != null)
            {

                nut = new Nut { Database = WellKnownDb.AppConfiguration, Table = container, Key = name, Properties = new Dictionary<string, string>() };

                nut.Value = result["value"] as string;
                nut.Uri = string.Format("/{0}/{1}/{2}", account, container, name);

                foreach (var j in result.Keys)
                {
                    nut.Properties.Add(j, result[j] as string);
                }

            }

            return nut;
        }
        public virtual object DoCheck()
        {
            Document commandResults = null;
            Mongo mongo = null;

            try
            {
                mongo = new Mongo(_connectionString);
                mongo.Connect();

                // In the Linux agent, we get all database names and use the first
                // one found, but no such functionality exists with this .NET
                // MongoDB library.
                Database database = mongo.GetDatabase(DatabaseName);
                commandResults = database.SendCommand("serverStatus");
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return null;
            }
            finally
            {

                if (mongo != null)
                {
                    mongo.Disconnect();
                }

            }

            if (commandResults == null)
            {
                Log.Warn("MongoDB returned no results for serverStatus command.");
                Log.Warn("This is possible on older versions of MongoDB.");
                return null;
            }

            Document indexCounters = (Document)commandResults["indexCounters"];
            Document btree = null;

            // Index counters are currently not supported on Windows.
            if (indexCounters["note"] == null)
            {
                btree = (Document)indexCounters["btree"];
            }
            else
            {
                // We add a blank document, since the server is expecting
                // these btree index values to be present.
                btree = new Document();
                indexCounters.Add("btree", btree);
                btree.Add("accesses", 0);
                btree.Add("accessesPS", 0);
                btree.Add("hits", 0);
                btree.Add("hitsPS", 0);
                btree.Add("misses", 0);
                btree.Add("missesPS", 0);
                btree.Add("missRatio", 0D);
                btree.Add("missRatioPS", 0);
            }

            Document opCounters = (Document)commandResults["opcounters"];
            Document asserts = (Document)commandResults["asserts"];

            if (_mongoDBStore == null)
            {
                Log.Debug("No cached data, so storing for the first time.");

                btree["accessesPS"] = 0;
                btree["hitsPS"] = 0;
                btree["missesPS"] = 0;
                btree["missRatioPS"] = 0;

                opCounters.Add("insertPS", 0);
                opCounters.Add("queryPS", 0);
                opCounters.Add("updatePS", 0);
                opCounters.Add("deletePS", 0);
                opCounters.Add("getmorePS", 0);
                opCounters.Add("commandPS", 0);

                asserts.Add("regularPS", 0);
                asserts.Add("warningPS", 0);
                asserts.Add("msgPS", 0);
                asserts.Add("userPS", 0);
                asserts.Add("rolloversPS", 0);
            }
            else
            {
                Log.Debug("Cached data exists, so calculating per sec metrics.");

                Document cachedBtree = (Document)((Document)_mongoDBStore["indexCounters"])["btree"];
                Document cachedOpCounters = (Document)_mongoDBStore["opcounters"];
                Document cachedAsserts = (Document)commandResults["asserts"];

                btree["accessesPS"] = (float)(((int)btree["accesses"] - (int)cachedBtree["accesses"]) / 60);
                btree["hitsPS"] = (float)(((int)btree["hits"] - (int)cachedBtree["hits"]) / 60);
                btree["missesPS"] = (float)(((int)btree["misses"] - (int)cachedBtree["misses"]) / 60);
                btree["missRatioPS"] = (float)(((double)btree["missRatio"] - (double)cachedBtree["missRatio"]) / 60);

                opCounters.Add("insertPS", (float)(((int)opCounters["insert"] - (int)cachedOpCounters["insert"]) / 60));
                opCounters.Add("queryPS", (float)(((int)opCounters["query"] - (int)cachedOpCounters["query"]) / 60));
                opCounters.Add("updatePS", (float)(((int)opCounters["update"] - (int)cachedOpCounters["update"]) / 60));
                opCounters.Add("deletePS", (float)(((int)opCounters["delete"] - (int)cachedOpCounters["delete"]) / 60));
                opCounters.Add("getmorePS", (float)(((int)opCounters["getmore"] - (int)cachedOpCounters["getmore"]) / 60));
                opCounters.Add("commandPS", (float)(((int)opCounters["command"] - (int)cachedOpCounters["command"]) / 60));

                asserts.Add("regularPS", (float)(((int)asserts["regular"] - (int)cachedAsserts["regular"]) / 60));
                asserts.Add("warningPS", (float)(((int)asserts["warning"] - (int)cachedAsserts["warning"]) / 60));
                asserts.Add("msgPS", (float)(((int)asserts["msg"] - (int)cachedAsserts["msg"]) / 60));
                asserts.Add("userPS", (float)(((int)asserts["user"] - (int)cachedAsserts["user"]) / 60));
                asserts.Add("rolloversPS", (float)(((int)asserts["rollovers"] - (int)cachedAsserts["rollovers"]) / 60));
            }

            _mongoDBStore = commandResults;

            return _mongoDBStore;
        }