MongoDB下载
官网:https://www.mongodb.com/download-center
MongoDB客户端
推荐使用RoboMongle,下载地址:https://robomongo.org/
安装MongoDB
1、安装Visual C++运行环境。
Microsoft Visual C++ 2015 Redistributable Update 3_x64.exe
Microsoft Visual C++ 2015-2019 Redistributable_x64.exe
2、解压MongoDB。
使用7zip、winrar等解压缩MongoDB.zip。
3、安装MongoDB服务。
以管理员身份启动cmd,执行以下命令:
mongod --dbpath=D:\MongoDB\db --logpath=D:\mongodb\log\logs.txt --install --serviceName MongoDB
4、启动MongoDB服务。
在Windows服务管理器中启动MongoDB服务。
5、安装MongoDB客户端。
安装MongoDB客户端studio-3t,支持可视化导入数据。
常用MongoDB命令
安装服务
mongod --dbpath=D:\mongodb\bin\db --logpath=D:\mongodb\bin\log\mongoDB.log --install --serviceName MongoDB
卸载服务
mongod.exe --remove --serviceName MongoDB
启动服务
net start MongoDB
停止服务
Net stop MongoDB
启动MongoDB(非服务)
mongod.exe --dbpath=D:\mongodb\bin\db
MongoDB备份
mongodump /host:localhost /port:27017 /db:Learn /out:dump
MongoDB还原
mongorestore /host:localhost /port:27017 /db:Learn /dir:dump\Learn /drop
MongoDB导入
mongoimport /host:localhost /port:27017 /db:Learn /collection:MyCollection /file:data.json /jsonArray /drop
MongoDB导出
mongoexport /host:localhost /port:27017 /db:Learn /collection:MyCollection /out:data.json /jsonArray /pretty
MongoDB导出带条件
mongoexport /host:localhost /port:27017 /db:Learn /collection:MyCollection /query:"{x:{$gte:4}}" /out:data.json /jsonArray /pretty
常用MongoDB脚本
相等查询
db.Default.find({ 'geometry.type' : 'Point' });
db.getCollection('Default').find({ 'geometry.type' : 'Point' });
db.Default.find({ 'geometry.type' : {$eq: 'Point'} });
db.Default.find({ '_id' : ObjectId("58aa9c95a914a16810251f88") });
不相等查询
db.Default.find({ 'geometry.type' : {$ne: 'Point'} });
比较查询
db.PersonLocation.find({ AddTime:{ $gt: ISODate('2017-03-20') } });
db.PersonLocation.find({ AddTime:{ $lt: ISODate('2017-03-20') } });
db.PersonLocation.find({ AddTime:{ $gte: ISODate('2017-03-20') } });
db.PersonLocation.find({ AddTime:{ $lte: ISODate('2017-03-20') } });
包含和不包含查询
db.Default.find({ 'properties.DBName' : {$in: ['Pipe', 'Device']} });
db.Default.find({ 'properties.DBName' : {$nin: ['Pipe', 'Device']} });
Like查询
db.Default.find({ 'properties.DBName' : /ip/ }); -------相当于like ‘%ip%’
db.Default.find({ 'properties.DBName' : /.ip./ }); -----------ip两边必须有字符
db.Default.find({ 'properties.DBName' : /^Pip/ }); --------------必须以Pip开头
分组查询
db.AutoCheck.aggregate([{$match:{'AddTime':{'$gte':ISODate('2017-03-25'),'$lte':ISODate('2017-03-25 23:59:59')}}},{$group:{_id:{PipeTypeID:'$PipeTypeID',PersonID:'$PersonID',PipeTypeCode:'$PipeTypeCode'},total:{$sum: '$PipeLength'}}}]);
更新文档
db.Default.update({DataId:{$eq:ObjectId('58aa9c37a914a1681024d28b')}}, {$set:{'properties.length ':100}}, {multi: true });
db.RegulatorStation.update({ 'properties.DBName': 'Device'},{$set: { 'properties.DBName ' : 'RegulatorStation '} }, false, true );
建立地理索引
db.CheckPoint.ensureIndex({ geometry: "2dsphere" });
查询附近点
db.Default.find({
geometry: {
$near: {
$geometry: {
type: 'Point',
coordinates: [116.587789406048, 35.395918133799]
},
$maxDistance: 50,
$minDistance: 1
}
}
});
范围查询
db.Default.find({geometry:{$geoWithin:{$geometry:{type:'Polygon',coordinates:[[[116.55808031558991,35.4438740258138],[116.58075168728828,35.445975022714478],[116.59775454550982,35.454980800280154],[116.61207117140293,35.4541057626403],[116.62309907376766,35.453629595057514],[116.63342323154211,35.445118733796754],[116.64366625249386,35.435794018212917],[116.63929626345635,35.413569542951294],[116.62958381697536,35.383415345424275],[116.59898618236184,35.3564292278231],[116.56320014968514,35.3521846360256],[116.54416350647807,35.381063688869155],[116.53083829209209,35.403458227444183],[116.53171621263027,35.41687152727819],[116.53685817494988,35.428331992853423],[116.55808031558991,35.4438740258138]]]}}}})
综合查询
db.Default.find({'geometry.type':'LineString','properties.rq':{$exists:true},geometry:{$geoWithin:{$geometry:{type:'Polygon',coordinates:[[[116.55808031558991,35.4438740258138],[116.58075168728828,35.445975022714478],[116.59775454550982,35.454980800280154],[116.61207117140293,35.4541057626403],[116.62309907376766,35.453629595057514],[116.63342323154211,35.445118733796754],[116.64366625249386,35.435794018212917],[116.63929626345635,35.413569542951294],[116.62958381697536,35.383415345424275],[116.59898618236184,35.3564292278231],[116.56320014968514,35.3521846360256],[116.54416350647807,35.381063688869155],[116.53083829209209,35.403458227444183],[116.53171621263027,35.41687152727819],[116.53685817494988,35.428331992853423],[116.55808031558991,35.4438740258138]]]}}}},{'properties.rq':1,'properties.length':1})
MapReduce查询
db.PersonLocation.mapReduce(function () {
emit(this.Person.PersId, this);
}, function (key, values) {
var newest = null;
if (values.length > 0) {
newest = values[0];
}
for (var i = 0; i < values.length; i++) {
var value = values[i];
if (value.AddTime > newest.AddTime) {
newest = value;
}
}
return newest;
}, {
out: 'PersonNewest',
query: {
AddTime: {
$gte: ISODate("2017-04-06"),
$lte: ISODate("2017-04-07")
}
}
}).find({});
相关网站
MongoDB官网:https://www.mongodb.com/
MongoDB手册:https://docs.mongodb.com/manual/
.Net驱动参考:http://mongodb.github.io/mongo-csharp-driver/2.4/
.Net驱动API:http://mongodb.github.io/mongo-csharp-driver/2.4/apidocs/
MongoDB客户端:https://robomongo.org/