public Nullable <DateTime> GetDateOfDeviceLastVisit(Guid deviceId, Guid applicationId)
        {
            try
            {
                IMongoQuery queryBase = Query.And
                                        (
                    Query <AppUsageSummary> .EQ <Guid>(mem => mem.ApplicationId, applicationId),
                    Query.EQ("DevicesVisits.DeviceId", BsonValue.Create(deviceId))
                                        );

                MongoCursor <AppUsageSummary> appUsageSummaries =
                    this.GetCollection <AppUsageSummary>().Find(queryBase);

                appUsageSummaries.SetSortOrder(SortBy <AppUsageSummary> .Descending(x => x.Date));
                AppUsageSummary appUsageSummary = appUsageSummaries.FirstOrDefault();

                if (appUsageSummary != null)
                {
                    return(appUsageSummary.Date);
                }

                return(null);
            }
            catch (Exception ex)
            {
                throw new DataAccessLayerException(ex);
            }
        }
Esempio n. 2
0
        /*
         * The application uses local installation of MongoDb
         * Database name: "Dictionary"
         * Table with entries name: "Entry"
         */

        public static void Main()
        {
            string          mongoDbConnectionString = Settings.Default.MongoDbConnectionString;
            MongoDatabase   mongoDb      = Connector.GetDb(mongoDbConnectionString, "Dictionary");
            MongoCollection entryMongoDb = mongoDb.GetCollection("entry");

            Console.WriteLine(new string('*', 20));
            Console.WriteLine("Please enter a word:");
            string newWord = Console.ReadLine();

            Console.WriteLine(new string('*', 20));
            Console.WriteLine("Please enter the word translation:");
            string newWordTranslation = Console.ReadLine();

            BsonDocument newEntry = new BsonDocument()
            {
                { "word", newWord },
                { "translation", newWordTranslation }
            };

            entryMongoDb.Insert(newEntry);

            Console.WriteLine(new string('*', 20));
            Console.WriteLine("All words in the dictionary are:");
            MongoCursor <BsonDocument> allWordsInDicitonary = mongoDb.GetCollection("entry").FindAll();

            foreach (var word in allWordsInDicitonary)
            {
                string wordText        = word.Elements.Single(x => x.Name == "word").Value.ToString();
                string wordTranslation = word.Elements.Single(x => x.Name == "translation").Value.ToString();
                Console.WriteLine("{0} - {1}", wordText, wordTranslation);
            }

            Console.WriteLine(new string('*', 20));
            Console.WriteLine("Please enter the word you are searching for:");
            string searchedWord = Console.ReadLine();

            var searchedWordResult = allWordsInDicitonary.FirstOrDefault(x => x.Elements.Single(e => e.Name == "word").Value.ToString() == searchedWord);

            if (searchedWordResult != null)
            {
                Console.WriteLine("Your word translation is: {0}", searchedWordResult.Single(x => x.Name == "translation").Value.ToString());
            }
            else
            {
                Console.WriteLine("No such word in the dictionary");
            }
        }
        protected void Submit_Click(object sender, EventArgs e)
        {
            const string udcErrorMethod = "Submit_Click";
            BsonDocument bsonDoc        = new BsonDocument();
            clsUserInfo  objMongoData;

            try
            {
                string strUserName = txtUserName.Text.Trim();
                string strPass     = txtUserPass.Text.Trim();

                if (!string.IsNullOrWhiteSpace(strUserName) && !string.IsNullOrWhiteSpace(strPass))
                {
                    var userId = Query.EQ("_id", strUserName);
                    MongoCursor <BsonDocument> cursor = objMongo.bsonReadDocument(strCollectionName, userId);
                    if (cursor != null && cursor.Count() > 0)
                    {
                        bsonDoc      = cursor.FirstOrDefault();
                        objMongoData = BsonSerializer.Deserialize <clsUserInfo>(bsonDoc.ToJson());
                        clsLog.blnLogDebug(udcErrorSource, udcErrorMethod, "User Data successfully fetched from mongo database", "", "", "", "");
                        if (strPass.Equals(objMongoData.Password.Trim()))
                        {
                            //ClientScript.RegisterClientScriptBlock(this.GetType(), "Successfully Saved", "<script language=javascript>alert('Successfully logged in!!!');</script>");
                            //Response.Redirect("/Web Forms/RoomLayout.aspx");
                            Session["username"] = objMongoData._id;
                            Server.Transfer("/Web Forms/RoomLayout.aspx");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "Invalid Password", "<script language=javascript>alert('Invalid password');</script>");
                            clsLog.blnLogError(udcErrorSource, udcErrorMethod, "User Password is not correct", "");
                        }
                    }
                    else
                    {
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "Invalid User ID or Password", "<script language=javascript>alert('Invalid User ID or Password');</script>");
                        clsLog.blnLogError(udcErrorSource, udcErrorMethod, "UserData not received from Mongo database or | User does not exist", "");
                    }
                }
            }
            catch (Exception ex)
            {
                clsLog.blnLogError(udcErrorSource, udcErrorMethod, "Exception catched in method blnValidateLogin !! ", " | Exception : " + ex.ToString());
            }
        }
Esempio n. 4
0
        public T Get(Expression <Func <T, bool> > filter, Dictionary <string, sbyte> sortByMap)
        {
            MongoCursor <T> cursor = null;

            if (null != filter)
            {
                var findQuery = Query <T> .Where(filter);

                cursor = this._mongoCollection.FindAs <T>(findQuery);
            }
            else
            {
                cursor = this._mongoCollection.FindAllAs <T>();
            }

            if (null != sortByMap)
            {
                var orderByQuery = new SortByDocument(sortByMap);
                cursor = cursor.SetSortOrder(orderByQuery);
            }

            return(cursor.FirstOrDefault());
        }