/// <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); }
/// <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 mongodbproxy(String ip, short port, String db, String collection) { var setting = new MongoDB.Driver.MongoServerSettings(); setting.Server = new MongoDB.Driver.MongoServerAddress(ip, port); _mongoserver = new MongoDB.Driver.MongoServer(setting); var _db = _mongoserver.GetDatabase(db); _collection = _db.GetCollection <MongoDB.Bson.BsonDocument> (collection); }
protected void Unnamed1_Click(object sender, System.EventArgs e) { string fileName = @"D:\stefan.steiger\Downloads\keywords.xlsx"; MongoDB.Driver.MongoClient client = new MongoDB.Driver.MongoClient("mongodb://localhost/27017/mydb"); MongoDB.Driver.MongoServer server = client.GetServer(); MongoDB.Driver.MongoDatabase database = server.GetDatabase("testdb"); MongoGridFs gridFs = new MongoGridFs(database); MongoDB.Bson.ObjectId id = MongoGridFsUsage.UploadFile(gridFs, fileName); // byte[] baFile = DownloadFile(gridFs, id); // DownloadFile(gridFs, id, @"d:\test.rar"); }
// http://odetocode.com/blogs/scott/archive/2013/04/16/using-gridfs-in-mongodb-from-c.aspx public static void Test() { string fileName = "clip_image071.jpg"; MongoDB.Driver.MongoClient client = new MongoDB.Driver.MongoClient(); MongoDB.Driver.MongoServer server = client.GetServer(); MongoDB.Driver.MongoDatabase database = server.GetDatabase("testdb"); MongoGridFs gridFs = new MongoGridFs(database); MongoDB.Bson.ObjectId id = UploadFile(gridFs, fileName); byte[] baFile = DownloadFile(gridFs, id); DownloadFile(gridFs, id, @"d:\test.rar"); } // End Sub Test
public void Mongo020DB() { try { String connString = "mongodb://" + Program.MongoData.Host + ":" + Program.MongoData.Port; Console.WriteLine("Creating client..."); Console.WriteLine(connString); MongoDB.Driver.MongoClient _client = new MongoDB.Driver.MongoClient(connString); Console.WriteLine("Getting Server..."); MongoDB.Driver.MongoServer _server = _client.GetServer(); Console.WriteLine("Getting database " + Program.MongoData.Database + "..."); MongoDB.Driver.MongoDatabase _database = _server.GetDatabase(Program.MongoData.Database); Console.WriteLine(_database.Name); } catch (Exception exc) { new frmException(exc, "You are not running MongoDB on " + Program.MongoData.Host + ":" + Program.MongoData.Port + "." + Environment.NewLine + "Or you do not have rights to list databases.").ShowDialog(); Console.WriteLine("Connection UnSuccessful."); } }
public void InitializeDatabaseConnection(String connectionString, String dbname) { client = new MongoDB.Driver.MongoClient(connectionString); server = client.GetServer(); Database = server.GetDatabase(dbname); }