9.5更新:更方便的启动命令
1)在D:\MongoDB中新建mongo.config文件,内容为
#启动mongod:mongod.exe --bind_ip 127.0.0.1 --logpath D:\MongoDB\Logs\mongodb.log --logappend --dbpath D:\MongoDB\db --service
#或安装为服务:mongod --config D:\mongodb\mongo.config --service
#SC方式安装服务:sc create mongodb binPath= "D:\MongoDB\Server\3.2\bin\mongod.exe --service --config=D:\MongoDB\mongo.config"
#auth=true ## 是否启用权限控制
dbpath=D:\MongoDB\db ## 数据文件路径
logpath=D:\MongoDB\Logs\mongodb.log ## 日志文件路径
logappend=true ## 是否
journal=true ## 是否启用日志文件
bind_ip=127.0.0.1 ## 绑定本机ip仅允许本机访问
port=27017 ## 绑定端口
2)在D:\MongoDB\Logs中建立mongodb.log保存日志
3)运行“D:\MongoDB\Server\3.2\bin\mongod.exe --config D:\mongodb\mongo.config --service”创建服务(需要修改服务可以使用sc delete servername删除服务)
--------------------------------------------------------------------------------------------------------------------------
8.24更新:
再分享一个网友的MongoDB项目例子,里面代码很清晰,注意下里面的驱动版本~
http://download.csdn.net/detail/yyl8781697/5335249
--------------------------------------------------------------------------------------------------------------------------
8.12更新:
找到一个第三方封装组件: MongoRepository,最新支持官方驱动1.11.0,并很好的支持LinQ,开发者写的文档也很详细,推荐使用~
NuGet地址:https://www.nuget.org/packages/MongoRepository/
GitHub:https://github.com/RobThree/MongoRepository
入门文档:https://github.com/RobThree/MongoRepository/wiki/Documentation
--------------------------------------------------------------------------------------------------------------------------
最近项目要求做一个主要业务的操作日志,保存后供追溯查询。由于需要较高的写入性能,且不用繁琐的关系存储,思来想去nosql适合。
在比较redis和mongodb后决定采用后者,原因只有一个,后者支持方便的条件查询……
1、先说环境搭建(Windows 10),先进入官网https://www.mongodb.com/download-center?jmp=nav#community下载最新的3.2版本,是否需要with SSL看项目需求。
2、下载完成直接点击安装,安装完成后:
1):手动创建dbpath路径
2):将mongodb安装为service,以admin权限启动cmd输入命令:
mongod.exe --bind_ip 127.0.0.1 --logpath "D:\MongoDB\dbConf\mongodb.log" --logappend --dbpath "D:\MongoDB\db" --serviceName "MongoDB" --serviceDisplayName "MongoDB" --install
其中, bind_ip:指定访问数据的主机ip,这里仅限本地;
logpath:log文件路径,自主指定;
logappend:追加形式写log;
dbpath:即上面创建好的dbpath;
serviceName:服务名称;
serviceDisplayName;显示的服务名称;
3)、启动服务:net start MongoDB
3、进入安装目录运行mongo即可打开console,常用命令:
Show dbs-------所有数据库
Use [dbname] -------进入指定数据库
Show collections-------显示所有集合(相当于tsql的table)
Db.[collectionname].find()-------查询集合所有数据
Db.[collectionname].drop()-------删除集合,如果当前只有一个集合,那么将删除db
其他命令可分别使用db.help()和db.[collectionname].help()查询
4、环境配置完毕,添加驱动,这里使用的是官方最新1.11版,可以在VisualStudio中使用NuGet工具直接添加:
1)在NuGet中搜索mongodb,请选择第二个mongocsharpdriver;
2)添加完毕后会多出两个dll:MongoDB.Driver.dll和MongoDB.Bson.XML;
3)官方1.x和2.x驱动的api是不同的,要注意。
5、驱动使用方法见如下provider,由于业务层使用了泛型,这里用了比较多的判断:
using _5yue.Common; using _5yue.Models; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5yue.Data.MongoDB { public class MongoDBProvider { private static string serverAddress = "mongodb://localhost:27017"; private static string mongoDbName = "db_name"; //private MongoServer server; private MongoClient client = null; private MongoDatabase database = null; private MongoCollection collection = null; public MongoDBProvider(string collectionName) { if (collection == null) { client = new MongoClient(serverAddress);// 打开连接 database = client.GetServer().GetDatabase(mongoDbName);// 打开数据库 collection = database.GetCollection(collectionName);// 打开集合(相当于表) } else if (!collection.Name.Equals(collectionName)) { collection = database.GetCollection(collectionName);// 更换集合 } } #region 插入 public void InsertObj<T>(T document)// 单条 { collection.Insert<T>(document); } public void InsertBson(BsonDocument bDoc)// 插入单条bson { collection.Insert(bDoc); } public void InsertObjList<T>(IList<T> objs)// 多条 { collection.InsertBatch<T>(objs); } #endregion #region 查询 public MongoCursor<T> GetAll<T>() { return collection.FindAllAs<T>(); } /// <summary> /// 通过条件获取结果 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="user"></param> /// <param name="entityId"></param> /// <param name="minTime"></param> /// <param name="maxTime"></param> /// <returns></returns> public MongoCursor<T> GetByCondition<T>(OperateLogModel olModel) { if (olModel != null && parameterNameList != null && parameterNameList.Count > 0) { List<IMongoQuery> list = new List<IMongoQuery>(); list.Add(Query.EQ("paras", olModel.operatorId));// paras:db中存放的字段,olModel.operatorId: 传过来的查询条件 if (list.Count > 0) { var query = Query.And(list);// 条件连接 //return collection.FindAs<T>(Query.EQ("CreaterId", 2)); return collection.FindAs<T>(query); } else { return collection.FindAllAs<T>(); } } else return null; } #endregion #region 工具 public void ChangeCollection(string collectionName) { collection = database.GetCollection(collectionName); } public MongoDatabase GetDB() { return database == null ? database : null; } public MongoCollection GetCollection() { return collection == null ? collection : null; } /// <summary> /// 把MongoCursor转换成List类型 /// </summary> /// <param name="cursor">文档游标</param> /// <returns></returns> public List<T> CursorToList<T>(MongoCursor<T> cursor) { List<T> resultList = new List<T>(); cursor.GetEnumerator().MoveNext();// 设置游标 if(cursor.Count() > 0) { foreach (var obj in cursor)// 遍历 { resultList.Add(obj); } } return resultList; } #endregion } }