Esempio n. 1
0
        /// <summary>
        /// 根据ObjectID 查询
        /// </summary>
        public BsonDocument LogSelectOne(string objId)
        {
            BsonDocument docFind = new BsonDocument();

            MongoDB.Driver.MongoServer server = MongoDB.Driver.MongoServer.Create(ConfigurationManager.AppSettings["mongoDBConfig"]);
            server.Connect();
            //获取指定数据库
            MongoDB.Driver.MongoDatabase db = server.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"].ToString());
            //获取表
            MongoDB.Driver.MongoCollection <BsonDocument> col = db.GetCollection <BsonDocument>(ConfigurationManager.AppSettings["mongoDBCollection"].ToString());

            try
            {
                var query = new MongoDB.Driver.QueryDocument("_id", new ObjectId(objId));
                docFind = col.FindOne(query);
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
            finally
            {
                server.Disconnect();
            }
            return(docFind);
        }
Esempio n. 2
0
        /// <summary>
        /// 根据ObjectID 查询
        /// </summary>
        public BsonDocument LogSelectOne(string objId)
        {
            BsonDocument docFind = new BsonDocument();

            MongoDB.Driver.MongoServer server = MongoDB.Driver.MongoServer.Create(ConfigurationManager.AppSettings["mongoDBConfig"]);
            server.Connect();
            //获取指定数据库
            MongoDB.Driver.MongoDatabase db = server.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"].ToString());
            string appid = string.Empty;

            if (!string.IsNullOrEmpty(txtAppid.Text.Trim()))
            {
                appid = txtAppid.Text.Trim().Split(' ')[0];
            }
            //获取表
            MongoDB.Driver.MongoCollection <BsonDocument> col = db.GetCollection <BsonDocument>(appid + "logs");

            try
            {
                var query = new MongoDB.Driver.QueryDocument("_id", new ObjectId(objId));
                docFind = col.FindOne(query);
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
            finally
            {
                server.Disconnect();
            }
            return(docFind);
        }
 public void Mongo000DefaultConnect()
 {
     try
     {
         Console.WriteLine("Creating client...");
         MongoDB.Driver.MongoClient _client = new MongoDB.Driver.MongoClient();
         Console.WriteLine("Getting Server...");
         MongoDB.Driver.MongoServer _server = _client.GetServer();
         Console.WriteLine("Connecting...");
         _server.Connect();
         Console.WriteLine("Connection Successful.");
     }
     catch (Exception exc)
     {
         new frmException(exc, "You are not running MongoDB on localhost:27017." + Environment.NewLine + "So it is failing.").ShowDialog();
         Console.WriteLine("Connection UnSuccessful.");
     }
 }
 public void Mongo020ConnectClientSettings()
 {
     try
     {
         Console.WriteLine("Creating client...");
         MongoDB.Driver.MongoClient _client = new MongoDB.Driver.MongoClient(new MongoDB.Driver.MongoClientSettings {
             Server = new MongoDB.Driver.MongoServerAddress(Program.MongoData.Host, Program.MongoData.Port)
         });
         Console.WriteLine("Getting Server...");
         MongoDB.Driver.MongoServer _server = _client.GetServer();
         Console.WriteLine("Connecting...");
         _server.Connect();
         Console.WriteLine("Connection Successful.");
     }
     catch (Exception exc)
     {
         new frmException(exc, "You are not running MongoDB on " + Program.MongoData.Host + ":" + Program.MongoData.Port + "." + Environment.NewLine + "So it is failing.").ShowDialog();
         Console.WriteLine("Connection UnSuccessful.");
     }
 }