您的位置:首页 > 数据库 > Mongodb

MongoDb笔记

2015-07-22 22:17 567 查看

设置

官网:https://www.mongodb.org/

教程:

1. 如何使用内存:http://www.oschina.net/question/16840_44521

2. MongoDb连接字符串标准:/article/4928013.html

C#官方的驱动:https://github.com/mongodb/mongo-csharp-driver

C#驱动教程: https://docs.mongodb.org/getting-started/csharp/

Java官方的驱动:http://mongodb.github.io/mongo-java-driver/

Java驱动教程: https://docs.mongodb.org/getting-started/java/

驱动有四个版本,目前还不清楚它们的区别, mongo-java-driver 的在线教程:http://mongodb.github.io/mongo-java-driver/3.2/driver/getting-started/quick-tour/

启动:/usr/bin/mongod -f /etc/mongod.conf

客户端工具

vue不能方便执行命令。推荐使用:

NoSql Manager :http://www.mongodbmanager.com/download

robomongo: http://robomongo.org/
更多工具: https://docs.mongodb.org/ecosystem/tools/administration-interfaces/

设置远程连接

打开: /etc/mongodb.conf ,修改 bind_ip=对外IP,或0.0.0.0 或注释掉(不能是 127.0.0.1)

重启: sudo service mongod restart

linux 下修改数据文件地址:

/etc/mongod.conf

修改: dbpath,logpath

一定要注意,指定的位置需要使用命令:

sudo chmod o+rwx mongo/

防火墙

禁用防火墙

centos 7:
systemctl stop firewalld.service #停止
systemctl disable firewalld.service #禁用

防火墙添加端口号

firewall-cmd --zone=public --add-port=27017/tcp --permanent

firewall-cmd --reload

如果添加Http端口,则:

firewall-cmd --permanent --zone=public --add-service=http

监控

http://blog.csdn.net/yyywyr/article/details/26816809

导出Json

mongoexport -h 192.168.8.161 -u ptr -p 1234 -d ptr -c resume -o abc.json

-h:数据库宿主机的IP

-u:数据库用户名

-p:数据库密码

-d:数据库名字

-c:集合的名字

-f:导出的列名

-q:导出数据的过滤条件

--csv:导出格式为csv

--quiet 安静模式,不显示进度

mongoimport -d local -c gis --type json --file likehua.data -f username,_id

命令行

参考:http://blog.csdn.net/shirdrn/article/details/7105539

登录: mongo -u ptr -p 1234 192.168.8.161/ptr

封装

先定义一个配置节

var where = new BsonDocument();

if (Mobile.HasValue())
{
where.Add(new BsonElement("Phone", BsonValue.Create(Mobile)));
}

if (IDCode.HasValue())
{
where.Add(new BsonElement("IdentityCode", BsonValue.Create(IDCode)));
}

if (Email.HasValue())
{
where.Add(new BsonElement("Email", BsonValue.Create(Email)));
}

var selectFields = Builders<mor.JobSeekerModel>.Projection.Include("_id");

var Ids = mor.JobSeekerInfo
.Find(new BsonDocumentFilterDefinition<mor.JobSeekerModel>(where))
.Project(selectFields)
.ToListInline()
.Select(o => o.GetValue("_id").AsString)
.ToArray()
;


View Code

JavaScript

Mongo集成Javascript才是真正的逆天。

where

db.jianli.find( "this.JianLiContentTXT.length > 2000" ).limit(1)

=

db.jianli.find(function(){ return this.JianLiContentTXT.length > 2000 ;} ).limit(1)

优化

1. 使用 ObjectId 类型的 _id

2. 遍历集合时,使用 _id 排序 比 无排序 更快。

http://www.jb51.net/article/62654.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: