MongoDB学习(一)
来源:互联网 发布:淘宝鱼竿特价 编辑:程序博客网 时间:2024/06/05 00:58
MongoDB是NoSql中一直使用比较广泛的数据库,闲来无事,学习学习
至于如何安装,配置MongoDB的环境请查看:http://cn.docs.mongodb.org/manual/installation/,里面基本的CRUD也详细介绍了,仔细看看
我是安装了Window7 64位使用的版本。
由于我使用C#,所以还得去下个MongoDB的C#下的驱动,请查看:http://www.mongodb.org/display/DOCS/CSharp+Language+Center
安装好驱动后,把里面的MongoDB.Driver.dll,MongoDB.BSon.dll引用到工程中,这样就万事具备了,开始开工了
简单的插入和查询数据:
class Person { public ObjectId _id;//BsonType.ObjectId//这个对应了 MongoDB.Bson.ObjectId 对目前的 mongdb 来说必须要有这个 public int Uid; public string Name; public string PassWord; } class Program { static void Main(string[] args) { string connectionString = "mongodb://localhost"; string databasename = "myDatabase"; string collectionName = "myCollection"; MongoClient client = new MongoClient(connectionString); MongoServer server = client.GetServer(); MongoDatabase database = server.GetDatabase("test"); // MongoCollection<Entity> collection = database.GetCollection<Entity>("entities"); //Entity entity = new Entity { Name = "Tom" }; //collection.Insert(entity); //var id = entity.Id; //Console.WriteLine(id); //var query = Query.EQ("_id", id); //entity = collection.FindOne(query); //entity.Name = "Dick"; //collection.Save(entity); //var update = Update.Set("Name", "Harry"); //collection.Update(query, update); //entity = collection.FindOne(query); //Console.WriteLine(entity.Name); //collection.Remove(query); MongoCollection mongoCollection = database.GetCollection("demo"); //先插入数据 MongoInsert(server, mongoCollection); //再查下数据,我暂时屏蔽了,等插入之后,就将插入那句给屏蔽,查询的这句放开 // MongoSelect(mongoCollection); Console.ReadKey(); } private static void MongoSelect(MongoCollection mongoCollection) { //更新字段名称,由于我在插入时,把第一行的Password的名字写错了,跟后面两行不同,就改下名称 var queryName = Query.EQ("Uid", 123); var updateName = Update.Rename("Password", "PassWord"); mongoCollection.Update(queryName, updateName); /*--------------------------------------------- * sql : SELECT * FROM table *--------------------------------------------- */ MongoCursor<Person> p = mongoCollection.FindAllAs<Person>(); //第一种读取的方式 foreach (var i in p) { Console.WriteLine("_id:{0}", i._id); Console.WriteLine(i.Uid + "\\" + i.Name + "\\" + i.PassWord); } /*--------------------------------------------- * sql : SELECT * FROM table WHERE Uid > 10 AND Uid < 20 *--------------------------------------------- */ QueryDocument query = new QueryDocument(); BsonDocument b = new BsonDocument(); b.Add("$gt", 100); b.Add("$lt", 124); query.Add("Uid", b); MongoCursor<Person> m = mongoCollection.FindAs<Person>(query); foreach (BsonDocument book in mongoCollection.FindAs<BsonDocument>(query)) //第二种读取的方式 { Console.WriteLine(book.ToString()); Console.WriteLine("_id:{0}", book["_id"].AsObjectId); Console.WriteLine(book["Uid"].AsInt32 + "\\" + book["Name"].AsString + "\\" + book["PassWord"].AsString); } /*----------------------------------------------- * sql : SELECT COUNT(*) FROM table WHERE Uid > 10 AND Uid < 20 *----------------------------------------------- */ long c = mongoCollection.Count(query); /*----------------------------------------------- * sql : SELECT Name FROM table WHERE Uid > 10 AND Uid < 20 *----------------------------------------------- */ FieldsDocument f = new FieldsDocument(); f.Add("Name", 1); MongoCursor<Person> mc = mongoCollection.FindAs<Person>(query).SetFields(f); /*----------------------------------------------- * sql : SELECT * FROM table ORDER BY Uid DESC LIMIT 10,10 *----------------------------------------------- */ SortByDocument s = new SortByDocument(); s.Add("Uid", -1); //-1=DESC MongoCursor<Person> ms = mongoCollection.FindAllAs<Person>().SetSortOrder(s).SetSkip(10).SetLimit(10); } private static void MongoInsert(MongoServer server, MongoCollection mongoCollection) { //普通插入 server.Connect(); var o = new { Uid = 123, Name = "xixiNormal", Password = "111111" }; mongoCollection.Insert(o); //对象插入 Person p = new Person { Uid = 124, Name = "xixiObject", PassWord = "222222" }; mongoCollection.Insert(p); //BsonDocument插入 BsonDocument b = new BsonDocument(); b.Add("Uid", 125); b.Add("Name", "xixiBson"); b.Add("PassWord", "333333"); mongoCollection.Insert(b); }
- MongoDB学习(一)
- MongoDB学习(一)
- mongoDB学习(一)
- mongodb学习(一)
- Mongodb学习(一)
- mongoDb学习整理(一)--Mongodb安装
- 学习MongoDB 四: MongoDB查询(一)
- MongoDB学习笔记(一)初识MongoDB
- MongoDB学习(一)MongoDB常用命令
- MongoDB 学习笔记(一)
- MongoDB 学习笔记(一)
- MongoDB学习笔记(一)
- mongodb学习笔记(一)
- MongoDB学习(一)介绍
- MongoDB学习笔记(一)
- MongoDB学习笔记(一)
- MongoDB学习笔记(一)
- mongodb初步学习(一)
- Flash AS3 获取当前域名
- js event.keycode 大全
- 3个著名加密算法(MD5、RSA、DES)的解析
- C++方面的Blog
- Android高手应该精通哪些内容?
- MongoDB学习(一)
- shell中 dd 命令
- 数学中常用希腊字母读法
- 谈谈对于企业级系统架构的理解
- windows下查杀端口占用进程的命令
- BlackBerry 10 SDK Gold版本地下载
- C#.对资源中的字符串换行
- js日期时间格式验证,时间比较
- design a HashMap for js