csharp: MongoDB

安装配置:

Install MongoDB on Windows(安装配置官方参考) http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/

1.Run MongoDB

C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe --dbpath d:\data\db

#配置数据库
mongod.exe --dbpath d:\data\db
#配置日志文件
mongod.exe --logpath D:\data\logs\mongodb.log --install

#测试用户登录

mongo -u geovindu -p

 

2.C# 连接字符串

<!--<add key="connectionString" value="Server=localhost:27017"/>-->
<!--<add key="connectionString" value="mongodb://localhost:27017"/>-->
<!--<add key="connectionString" value="Server=127.0.0.1:27017"/>-->
<add key="connectionString" value="mongodb://127.0.0.1:27017"/>


以上四项都可以

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using MongoDB.Bson;
using MongoDB.Driver;
 
namespace MongoDB2.Model
{
    /// <summary>
    /// Wrapper class to communicate with 'MyCompany' database.
    /// </summary>
    class MyCompany
    {
 
        /// <summary>
        ///
        /// </summary>
        public MyCompany()
        {
 
        }
 
        /// <summary>
        /// Connection string to the Mongo database server
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                return ConfigurationManager.AppSettings["connectionString"];
            }
        }
 
        /// <summary>
        /// Creates sample data for two collections(or tables) i.e, Departments, Employees.
        /// </summary>
        public static void CreateData()
        {
            CreateDepartments();
            CreateEmployees();
        }
         
        #region Departments
 
        /// <summary>
        /// Retrieve departments from MyCompany database.
        /// </summary>
        /// <returns></returns>
        public static List<Department> GetDepartments()
        {
            List<Department> lst = new List<Department>();
 
            MongoServer server = MongoServer.Create(ConnectionString);
            MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
            MongoDatabase myCompany = server.GetDatabase("geovinDB");//,credentials//MyCompany
            MongoCollection<Department> departments = myCompany.GetCollection<Department>("Departments");
            foreach (Department department in departments.FindAll())
            {
                lst.Add(department);
            }
 
            return lst;
        }
 
        /// <summary>
        /// Inserts sample departments data in MyCompany database
        /// </summary>
        private static void CreateDepartments()
        {
            string headOfDepartmentId;
 
            //insert department 'Development'
            headOfDepartmentId = "4f180083ef31ba0da8000010";
            CreateDepartment("Development", headOfDepartmentId);
 
            //insert department 'Accounts'
            headOfDepartmentId = "4f180083ef31ba0da8000011";
            CreateDepartment("Accounts", headOfDepartmentId);
 
            //insert department 'Human Resource'
            headOfDepartmentId = "4f180083ef31ba0da8000012";
            CreateDepartment("Human Resource", headOfDepartmentId);
        }
 
        /// <summary>
        /// Insert the department
        /// </summary>
        /// <param name="departmentName"></param>
        /// <param name="headOfDepartmentId"></param>
        private static void CreateDepartment(string departmentName, string headOfDepartmentId)
        {
            MongoServer server = MongoServer.Create(ConnectionString);
            MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
            MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials //MyCompany
 
            MongoCollection<BsonDocument> departments = myCompany.GetCollection<BsonDocument>("Departments");
            BsonDocument deptartment = new BsonDocument {
                        { "DepartmentName", departmentName },
                        { "HeadOfDepartmentId", headOfDepartmentId }
                        };
 
            departments.Insert(deptartment);
        }
 
        /// <summary>
        /// Delete all data in departments collection in MyCompany database
        /// </summary>
        public static void DeleteDepartments()
        {
            MongoServer server = MongoServer.Create(ConnectionString);
 
            MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
            MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//MyCompany
 
            MongoCollection<Department> departments = myCompany.GetCollection<Department>("Departments");
            departments.Drop();
        }
        #endregion
 
        #region Employees
 
        /// <summary>
        /// Retrieve employees from MyCompany database.
        /// </summary>
        /// <returns></returns>
        public static List<Employee> GetEmployees()
        {
            List<Employee> lst = new List<Employee>();
 
            MongoServer server = MongoServer.Create(ConnectionString);
            MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
            MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//无验证密码登录
 
            MongoCollection<Employee> employees = myCompany.GetCollection<Employee>("Employees");
            foreach (Employee employee in employees.FindAll())
            {
                lst.Add(employee);
            }
 
            return lst;
        }
 
        /// <summary>
        /// Inserts sample employees data in MyCompany database
        /// </summary>
        private static void CreateEmployees()
        {
            // add 5 sample Employees
            for (int i = 1; i <= 5; i++)
            {
                string departmentId = "4f180083ef31ba0da8000010";
                CreateEmployee("FirstName" + i, "LastName" + i, "Address" + i, "City" + i, departmentId);
            }
        }
 
        /// <summary>
        /// Insert the employee
        /// </summary>
        /// <param name="departmentName"></param>
        /// <param name="headOfDepartmentId"></param>
        private static void CreateEmployee(string firstName, string lastName, string address, string city, string departmentId)
        {
            MongoServer server = MongoServer.Create(ConnectionString);
            MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
            MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//MyCompany
 
            MongoCollection<BsonDocument> employees = myCompany.GetCollection<BsonDocument>("Employees");
            BsonDocument employee = new BsonDocument {
                        { "FirstName", firstName },
                        { "LastName", lastName },
                        { "Address", address },
                        { "City", city },
                        { "DepartmentId", departmentId }
                        };
 
            employees.Insert(employee);
        }
 
        /// <summary>
        /// Delete all data in employees collection in MyCompany database
        /// </summary>
        public static void DeleteEmployees()
        {
            MongoServer server = MongoServer.Create(ConnectionString);
            MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
            MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//MyCompany
 
            MongoCollection<Employee> employees = myCompany.GetCollection<Employee>("Employees");
            employees.Drop();
        }
        #endregion
    }
 
    #region Department
    /// <summary>
    /// Department represents a single item(record) stored in Departments collection.
    /// </summary>
    class Department
    {
        public ObjectId _id { get; set; }
        public string DepartmentName { get; set; }
        public ObjectId HeadOfDepartmentId { get; set; }
    }
    #endregion
 
    #region Employee
    /// <summary>
    /// Department represents a single item(record) stored in Employees collection.
    /// </summary>
    class Employee
    {
        public ObjectId _id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public ObjectId DepartmentId { get; set; }
    }
    #endregion
}

  

 

posted @   ®Geovin Du Dream Park™  阅读(594)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示