Mongo学习笔记(三)

mongo命令


基本验证

数字运算1+2

show dbs;显示数据库

use 数据库名;使用某个数据库;

db.dropDatabase();删除当前数据库

db.getName();查看当前数据库

db.stats(); 查看当前状态

db.getMongo();  查看当前连接地址

db.getPrevError();查看错误信息

db.resetError();删除错误信息


db.addUser("name");db.addUser("userName", "pwd123", true);添加用户、设置密码、是否只读

db.auth("userName", "123123");数据库认证、安全模式 结果1表示正确,

show users;显示当前所有用户show users;

db.removeUser("userName");删除用户


db.createCollection(“collName”, {size: 20, capped: 5, max: 100});创建固定集合,不可删除记录,查询优化

db.createCollection("testTow",{size:10,max:50}); 创建一个普通集合。可删除记录

db.getCollection("account");查看集合

db.getCollectionNames();显示当前数据库的所有集合

db.printCollectionStats();显示集合结构信息


集合本身属性操作

db.集合.help()对这个集合可以的操作

db.集合.count()集合记录个数

db.集合.dataSize();集合所占文件大小

db.集合.getDB()集合在哪个数据库实体中

db.集合.stats()集合的结构信息

db.userInfo.totalSize();得到聚集集合总大小

db.userInfo.storageSize();聚集集合储存空间大小

db.userInfo.getShardVersion()Shard版本信息

db.userInfo.renameCollection("users");集合重命名

db.userInfo.drop();删除集合


集合中的记录操作

db.集合.save({键值对格式,逗号分割})  集合插入记录

db.集合.find({键值对格式,多条件逗号分割})查询记录

db.集合.update(查询键值对,多条件逗号分割),{$set:{修改值键值对}}修改记录

db.集合.remove(键值对)删除记录

db.集合.remove()删除所有记录


查询修改删除

db.users.findAndModify(

{    query: {age: {$gte: 25}},    

    sort: {age: -1},    

    update: {$set: {name: 'a2'}, $inc: {age: 2}},

    remove: true});

db.runCommand({ findandmodify : "users",    

    query: {age: {$gte: 25}},

    sort: {age: -1},

    update: {$set: {name: 'a2'}, $inc: {age: 2}},

    remove: true});


集合记录查询

db.集合.find()查询所有

db.集合.distinct("name");查询结果去除某列重复的

db.userInfo.find({"age": 22});按条件查询

db.userInfo.find({age: {$gt: 22}});>22

db.userInfo.find({age: {$lt: 22}});<22

db.userInfo.find({age: {$gte: 25}});>=

db.userInfo.find({age: {$lte: 25}});<=

db.userInfo.find({age: {$gte: 23, $lte: 26}});>= <=

db.userInfo.find({name: /mongo/});包含%mongo%

db.userInfo.find({name: /^mongo/});包含mongo%

db.userInfo.find({}, {name: 1, age: 1});查询结果是name 和age量列的所有值

=db.集合.find({},{name:true,age:true,sex:false});

db.userInfo.find().sort({age: 1});升序

db.userInfo.find().sort({age: -1});降序

db.userInfo.find().limit(5);查询前5条记录

db.userInfo.find().skip(10);查询10条后的记录

db.userInfo.find().limit(10).skip(5);查询5-10的记录

db.userInfo.find({$or: [{age: 22}, {age: 25}]});or 查询

db.userInfo.find({sex: {$exists: true}}).count();查询是否存在sex值是true的记录,并计数


索引

db.userInfo.ensureIndex({name: 1});db.userInfo.ensureIndex({name: 1, ts: -1});

db.userInfo.getIndexes();

db.userInfo.totalIndexSize();

db.users.reIndex();

db.users.dropIndex("name_1");

db.users.dropIndexes();


语句块操作

基本运算:1+1

字符串连接 "h"+"s";

...

print(要打印的对象)

tojson(new Object('a'));将一个对象转成json对象


for循环,js格式

for(var i=0;i<30;i++){

print(i);

};


游标缓存查询结果

var cursor = db.users.find();

while (cursor.hasNext()) {    

printjson(cursor.next());

}将查询结果按照json格式显示

或者

var cursor = db.users.find();

for (var i = 0, len = c.length(); i < len; i++) printjson(c[i]);

数组

var arr = db.users.find().toArray();

printjson(arr[2]);


将查询结果安装json格式打印

db.users.find({age: {$lte: 28}}, {age: true}).forEach(printjson);


带参数传递的查询

db.things.find({x:4}).forEach(function(x) {print(tojson(x));});


导入导出

新起cmd窗口。录入mongoexport,进入mongoexport命令窗口。不能原来的mongo的窗口操作。


-h:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导出那些列

-o:指明到要导出的文件名

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

-csv:指明导出文件格式csv

默认json

mongoexport

./bin/mongoexport -d test -c students -o [路径]students.dat

导出test数据的students集合到当前路径下的students.dat文件,json格式。

./bin/mongoexport -d test -c students --csv -f classid,name,age -o students_csv.dat


mongoexport -d test -c testOne --csv -f name,age -o f:/testOne.csv

mongoimport

./bin/mongoimport -d test -c students students.dat

./bin/mongoimport -d test -c students --type csv --headerline --file students_csv.dat

--headerline 表示第一行是文件头,不要导入,否则会报异常


$(date +%F)为shell命令,会输出当前日期,格式为2012-02-22

mongoexport -d search_logs -c key_words -q '{request_time:{$gte:new Date(1329490800000)}}' -o mongo_$(date +%F).json输出带日期的文件  


mongo特性

固定集合与普通集合

capped Collection:性能好,能查询,更新,插入。不能删除,文件大小固定,超出大小则不能插入和更新。

 能够通过drop删除所有。

创建方式

db.createCollection("mycoll", {capped:true, size:100000, max:100});


查看是否是固定集合

db.集合.isCapped();true是

db.集合.validate();1表示可以插入文档


普通集合

db.runCommand({convertToCapped:'mycoll',size:10000,max:3})将一个普通的集合转为固定集合


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章