MongoDB使用

MongoDB使用

  • 部署服务
www.mongodb.com官网下载,安装后找到目录
–dpbath 指定数据库存放路径 –install 安装服务 –logpaht 指定日志路径 –host指定主机地址 –port 指定端口,默认27017
1 ::不装直接cmd窗口运行服务,差别就是一个--install,
2 mongod.exe --dbpath c:/mongodb/Data --logpath c:/MongoDb/Log/a.log
3 ::安装服务
4 mongod.exe --dbpath c:/mongodb/Data --install --logpath c:/MongoDb/Log/a.log

 

客户的连接
1 mongo.exe 192.168.1.100/demodb

 

=====================================================================================

  • C Sharp Part
直接NetGut添加驱动
ConnectionString
1 <add name="MongoServerSettings" connectionString="mongodb://192.168.1.100/27017"/>

 

main cs
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Configuration;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using MongoDB.Bson;
 8 using MongoDB.Driver.Linq;
 9 using MongoDB.Driver;
10 namespace MongoDbDemo
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             //获取连接字符串
17             string connStr = ConfigurationManager.ConnectionStrings["MongoServerSettings"].ConnectionString;
18             //创建client
19             MongoClient client = new MongoClient(connStr);
20             //获取数据库,如果没有,会自动创建
21             IMongoDatabase db = client.GetDatabase("DemoDb");
22             //指定集合名字
23             var collectionName = typeof(Customer).Name;
24             //获取集合,如果没有,自动创建
25             var collection = db.GetCollection<Customer>(collectionName);
26             //删除collection
27             //db.DropCollection("UserInfo");
28             //清空collection数据
29             //collection.DeleteMany(c => true);
30             #region 添加实体
31             //for (int i = 0; i < 100; i++)
32             //{//创建实体
33             //    Customer cus = new Customer();
34             //    cus.CusId = i;
35             //    cus.Name = "custom" + i;
36             //    cus.Subtime = DateTime.Now;
37             //    cus.Orders = new List<Order>
38             //    {
39             //        new Order(){Content = "order",OrderId = i},
40             //        new Order(){Content = "order2",OrderId = i}
41             //    };
42             //    cus.Demo = "ddd";
43             //    //加入到集合里
44             //    collection.InsertOne(cus);
45             //}
46             //Console.WriteLine(collection.Count(c => c.CusId > 50));
47             #endregion
48             #region 查询
49             //查询全部
50             //var data = collection.Find(c => true).ToList();
51             //foreach (var cus in data)
52             //{
53             //    Console.WriteLine(cus.Id + "------" + cus.CusId + "------" + cus.Name);
54             //}
55             //查询CusId大于10
56             //var data2 = collection.Find(new FilterDefinitionBuilder<Customer>().Gt(c => c.CusId, 10));
57             //var data2 = collection.Find(c => c.CusId > 10).ToList();
58             //foreach (var cus in data2)
59             //{
60             //    Console.WriteLine(cus.Id + "------" + cus.CusId + "------" + cus.Name);
61             //}
62             #endregion
63             #region 查询 Linq
64             var temp = collection.AsQueryable();
65             var data3 = from cus in temp
66                         where cus.CusId > 10 && cus.CusId < 50
67                         select cus.Name;
68             foreach (string s in data3)
69             {
70                 Console.WriteLine(s);
71             }
72             #endregion
73             Console.ReadKey();
74         }
75     }
76 }

 

customer.cs
 1 using System;
 2 using System.Collections.Generic;
 3 namespace MongoDbDemo
 4 {
 5     public class Customer:BaseEntity
 6     {
 7         public string Name { get; set; }
 8         public int CusId { get; set; }
 9         public DateTime Subtime { get; set; }
10         public List<Order> Orders { get; set; }
11         public string Demo { get; set; }
12     }
13     public class Order
14     {
15         public int OrderId { get; set; }
16         public string Content { get; set; }
17     }
18 }  

 

BaseEntity.cs
namespace MongoDbDemo
{
    public class BaseEntity
    {
        public object Id { get; set; }
    }
}  

 

posted on 2016-12-20 17:11  jmlsaul  阅读(92)  评论(0)    收藏  举报