MongoDB安装配置及CRUD操作

1.下载
https://www.mongodb.com/download-center
本文下载版本:Windows Server 2008 R2 64-bit

2.安装
默认安装路径:C:\Program Files\MongoDB\Server\3.2\bin

3.将MongoDB服务器作为Windows服务运行:
在E盘创建两个目录:
E:\MongoDB\log\
E:\MongoDB\data
控制台输入命令,创建服务:
<span style="font-size:18px;">mongod.exe --bind_ip 127.0.0.1 --logpath "E:\MongoDB\log\MongoDB.log" --logappend --dbpath "E:\MongoDB\data" --port 27017 --serviceName "MongoDBService" --serviceDisplayName "MongoDBDisplayNameOne" --install</span>

附:关闭服务和删除进程
 > d:\mongodb\bin>NET stop MongoDB   (关闭服务)
 > d:\mongodb\bin>mongod --dbpath "d:\mongodb\data\db" --logpath "d:\mongodb\data\log\MongoDB.log" --remove --serviceName "MongoDB"   
   (删除,注意不是--install了)

4.MongoDB的控制台操作:
进入mongoDB控制台:在mongodb的bin安装目录,输入mongo命令:
> mongo
MongoDB shell version: 3.0.6
connecting to: test
……

5.管理工具
http://rockmongo.com/downloads 下载RockMongo-on-Windows 
解压后启动:rockstart.bat  
默认用户名密码:admin
(http://127.0.0.1:7788/rockmongo/ )

6.C# .net操作

Nuget下载MongoDB包:


MongoDB操作类:

<span style="font-size:18px;">    class MongoDBHandle
    {
        private static string mongoDbServer = ConfigurationManager.AppSettings["mongoConnection"];
        private static string mongoDbName = ConfigurationManager.AppSettings["mongoDb"];
    
        protected static IMongoClient _client;
        protected static IMongoDatabase _database;
        private IMongoClient client;
        private IMongoDatabase database;
        //private IMongoCollection<T> collection;
        public MongoDBHandle()
        {

            if (_client == null)
                _client = new MongoClient(mongoDbServer);
            if (_database == null)
                _database = _client.GetDatabase(mongoDbName);
        }
        public static void getConnection()
        {
          
        }

        public async  void insertDB()
        {
            TestUser users = new TestUser();
            users.Name = "xumingxiang";
            users.Sex = "man";
            //获得Users集合,如果数据库中没有,先新建一个

            var collection = _database.GetCollection<TestUser>("restaurants");
            await collection.InsertOneAsync(users);
        }
        public async void queryDB()
        {
            var collection = _database.GetCollection<BsonDocument>("restaurants");
            //var filter = new BsonDocument();
            var filter = Builders<BsonDocument>.Filter.Eq("Name", "xumingxiang");//查询条件
            var count = 0;
            using (var cursor = await collection.FindAsync(filter))
            {
                while (await cursor.MoveNextAsync())
                {
                    var batch = cursor.Current;
                    foreach (var document in batch)
                    {
                        // process document
                        count++; //查询结果操作
                    }
                }
            }
        }
        public async void updateDB()
        {
            var collection = _database.GetCollection<BsonDocument>("restaurants");
            var filter = Builders<BsonDocument>.Filter.Eq("Name", "xumingxiang");//查询条件
            var update = Builders<BsonDocument>.Update.Set("Sex", "woman");//更新字段
            var result = await collection.UpdateOneAsync(filter, update);

        }

        public async void deleteDB()
        {
            var collection = _database.GetCollection<BsonDocument>("restaurants");
            var filter = Builders<BsonDocument>.Filter.Eq("Name", "xumingxiang");//查询条件
            var result = await collection.DeleteManyAsync(filter);//删除

        }

       
    }</span>

AppConfig配置:

<span style="font-size:18px;">  <appSettings>

    <add key="mongoConnection" value="mongodb://localhost:27017" />
    <add key="mongoDb" value="TestUserNew" />
  </appSettings></span>
TestUser类:

<span style="font-size:18px;">    public class TestUser
    {

            public ObjectId _id;//BsonType.ObjectId 这个对应了 MongoDB.Bson.ObjectId 
            public string Name { get; set; }
            public string Sex { set; get; }
        
    }</span>
调用:

<span style="font-size:18px;">        private void button1_Click(object sender, EventArgs e)
        {
            MongoDBHandle mongoDBHandle = new MongoDBHandle();
            mongoDBHandle.insertDB();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MongoDBHandle mongoDBHandle = new MongoDBHandle();
            mongoDBHandle.queryDB();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MongoDBHandle mongoDBHandle = new MongoDBHandle();
            mongoDBHandle.updateDB();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MongoDBHandle mongoDBHandle = new MongoDBHandle();
            mongoDBHandle.deleteDB();
        }</span>

用在线工具查看操作结果:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值