mongod --dbpath D:\MogonDB3.4.10\db //开启数据库,无端口号
mongod --dbpath D:\MogonDB3.4.10\db --port=10086 //开启数据库,有端口号
mongod -f D:\MogonDB3.4.10\mongodb.conf //开启数据库

删除集合:db.集合.drop()

//插入数组用[]
db.infos.insert([
{"url":"www.bf.cn"},
{"url":"www.bf.cn"}
]);

//使用js批量插入数据
for(var x=0;x<100;x++){
db.infos.insert({"url":"bf.cn-"+x});
};

//0表示不显示,1表示显示###.pretty()格式化
db.infos.find({“_id”:"789789"},{"_id":0,"name":1}).pretty();

db.students.drop();
db.students.insert({"name":"张三","sex":"男","age":18,"score":89,"address":"无锡"});
db.students.insert({"name":"李四","sex":"女","age":19,"score":99,"address":"苏州"});
db.students.insert({"name":"王五","sex":"男","age":22,"score":109,"address":"南京"});
db.students.insert({"name":"赵六","sex":"女","age":15,"score":82,"address":"新区"});
db.students.insert({"name":"孙七","sex":"男","age":22,"score":88,"address":"南宁"});
db.students.insert({"name":"王八","sex":"男","age":23,"score":85,"address":"长沙"});

//关系运算,>($gt),<($lt),>=($gte),<=($lte),!=($ne),==(key:value)
//bson,条件查询,查询年龄大于19的人
db.students.find({"age":{"$gt":19}}).pretty();
db.students.find({"age":{"$gte":19}}).pretty();// >=
db.students.find({"age":{"$ne":19}}).pretty(); //!=

//逻辑预算,与($and),或($or),非($nor,$not)
//查询年龄>=19且<=22的人
db.students.find({"age":{"$gte":19,"$lte":22}}).pretty();//与运算只需要利用","就可以实现
//查询年龄>=19或者成绩>=80的人
db.students.find({"$or":[
{"age":{"$gt":19}},
{"score":{"$gt":90}}]}).pretty();

//求模$(mod),语法"{$mod:[数字,余数]}"
db.students.find({"age":{"$mod":[20,2]}}).pretty();

//范围查询,$in(在范围之中),$nin(不在范围之中)
//名字是张三,李四的信息
db.students.find({"name":{"$in":["张三","李四"]}}).pretty();
//名字不是张三,李四的信息
db.students.find({"name":{"$nin":["张三","李四"]}}).pretty();

//插入新数据
db.students.insert({"name":"大神A","sex":"男","age":18,"score":89,"address":"无锡","course":["语文","数学"]});
db.students.insert({"name":"大神B","sex":"女","age":23,"score":90,"address":"广州","course":["英语","数学","政治"]});
db.students.insert({"name":"大神C","sex":"男","age":20,"score":99,"address":"南京","course":["语文","化学"]});
db.students.insert({"name":"大神D","sex":"女","age":17,"score":98,"address":"北京","course":["语文","生物"]});
db.students.insert({"name":"大神E","sex":"男","age":29,"score":97,"address":"上海","course":["语文","地理"]});
db.students.insert({"name":"大神F","sex":"女","age":22,"score":99,"address":"苏州","course":["语文","物理"]});
db.students.insert({"name":"大神G","sex":"男","age":24,"score":89,"address":"南宁","course":["语文","音乐"]});

//对数组数据进行判断,可以使用这几个运算符:$all,$size,$slice,$eleMatch
//例:同时参加语文、数学课程的学生
//俩个数组内容都需要保存,使用"{"$all":[内容1,内容2]}"
db.students.find({"course":{"$all":["语文","数学"]}}).pretty();
//查询数组中第二个内容(index=1,索引下标从0开始)为数学的信息
db.students.find({"course.1":"数学"}).pretty();
//查询只参加俩门课程的学生
db.students.find({"course":{"$size":2}}).pretty();
//查询年龄19岁的所有学生信息,但要求只显示俩门参加课程
db.students.find({"age":{"$gt":18}},{"course":{"$slice":2}}).pretty();
//【1,2】里面的第一个数据表示跳过的数据量,而第二个数据表示返回的数量
db.students.find({"age":{"$gt":18}},{"course":{"$slice":[1,2]}}).pretty();

//字段判断$exists
//查询具有parents成员的数据
db.students.find({"course":{"$exists":false}}).pretty();
db.students.find({"course":{"$exists":true}}).pretty();

//条件过滤$where
db.students.find({"$where":"this.age>20"}).pretty();
//等价于
db.students.find("this.age>20").pretty();
db.students.find( function(){
return this.age>20;
}).pretty();
db.students.find({"$where":function(){
return this.age>20;
}}).pretty();
//多条件查询使用$and,$where
db.students.find({"$and":[
{"$where":"this.age>19"},
{"$where":"this.age<22"}]}).pretty();

//正则运算
//基础语法:{key:正则标记}
//完整语法:{key:{"$regex":正则标记,"$option":选项}}
//--option 设置正则信息查询的标记
//----|- "i" :忽略字母大小写
//----|- "m" :多行查找
//----|- "x" :空白字符串除了被转义的或在字符串中以外的完全被忽略
//----|- "s" :匹配所有的字符(圆点、‘.’),包括换行内容
//查询以“大”开头的姓名
db.students.find({"name":/大/}).pretty();
//查询包含‘a’字符信息,
db.students.find({"name":{"$regex":/a/i}}).pretty();
//查询数组数据,做模糊查询使用
db.students.find({"course":/语?/}).pretty();
db.students.find({"course":/语/}).pretty();

//数据排序
//使用sort()函数,1表示升序,-1表示降序
db.students.find().sort({"score":-1}).pretty();
//自然排序,按照保存的先后顺序排序,$natural
db.students.find().sort({"$natural":-1}).pretty();

//分页显示
//skip(n):表示跨过多少行数据
//limit(n):取出的数据行的个数限制
//分页:第一页,skip(0).limit(5)
db.students.find().skip(0).limit(5).sort({"age":-1}).pretty();
//分页:第二页,skip(5).limit(5)
db.students.find().skip(0).limit(5).sort({"age":-1}).pretty();

//更新函数update(),save()
//语法:db.集合.update(更新条件,新的对象数据(更新操作符号),upsert,multi)
//---upsert:如果要更新的数据不存在,则增加一条新的内容(true为增加,false为不增加)
//---multi:表示是否只更新满足条件的第一行记录,如果设置为false,,只更新第一条,如果为true,全更新
//按自然顺序查询前5条
db.students.find().skip(0).limit(5).sort({"$natural":1}).pretty();
//只更新第一条数据:
db.students.update({"age":19},{"$set":{"score":100}},false,false);
//更新满足条件的所有数据:
db.students.update({"age":19},{"$set":{"score":100}},false,true);
//更新不存在的数据:
db.students.update({"age":19},{"$set":{"name":"你猜"}},true,false);

//修改器
//1.$inc:主要针对于一个数字字段,增加某个数字字段的数据内容
//----语法:{"$inc":{"成员":"内容"}}
//将所有年龄为19的学生成绩一律减少10分,年龄加1
db.students.update({"age":19},{"$inc":{"score":-10,"age":+1}});
//2.$set:进行内容的重新设置:
//----语法:{"$set":{"成员":"新内容"}}
//将年龄为20的人成绩修改为89
db.students.update({"age":20},{"$set":{"score":89}});
//3.unset:删除某个成员的内容
//----语法:{"unset":{"成员":1}}
//删除“张三”的年龄与成绩信息
db.students.update({"name":"张三"},{"$unset":{"age":1,"score":1}});
//4.$push:相当于将内容追加到指定的成员之中(基本上是数组)
//----语法:{"$push":{成员:value}}
//向“张三”添加课程信息(此时张三信息下没有course),如果没有数组,新建一个,没有,在后面追加
db.students.update({"name":"张三",{"$push":{"course":"化学"}}});
//5.$pushAll:与"$push"类似,可以一次追加多个内容到数组里面
//----语法:{"$pushAll":{成员:数组内容}}
//向“王五”信息添加课程内容
db.students.update({"name":"张三",{"$pushAll":{"course":["化学","素描"]}}});
//6.$addToSet:向数组里面增加一个新的内容,只有数组,#此时会判断要增加的内容
//在数组里面是否已经存在,如果存在不做操作,不存在,追加内容
db.students.update({"name":"王五"},{"$addToSet":{"course":"武术"}});
//7.$pop:删除数组内的内容
//----语法:{"$pop":{成员:内容}},内容如果设置为-1,表示删除第一个,1则表示删除最后一个
db.students.update({"name":"王五"},{"$pop":{"course":1}});
//8.$pull:从数组内删除一个指定内容的数据
//----语法:{"$pull":{成员:数据}},进行数据比对的,如果是此数据则删除
//删除音乐
db.students.update({"name":"王五","$pull":{"course":"音乐"}});
//9.$pullAll:一次性删除的多个内容
/----语法:{"$pullAll":{成员:[数据1,数据2]}},
db.students.update({"name":"王五","$pull":{"course":["音乐","英语"]}});
//10.$rename:为成员名称重命名
//----语法:{"$rename":{旧的成员名称:新的成员名称}}
//将张三的name名称修改为“姓名”
db.students.update({"name":"张三"},{"$rename":{"name":"姓名"}});

//删除数据remove()
//----删除条件:满足条件的数据被删除
//----是否只删除一个数据,如果设置为true或者是1表示只删除一个
db.infos.remove();//废弃,在2.X可用
//删除所有姓名带有‘大’的信息
db.students.remove({"$remove":/大/});
//删除一个姓名带有‘大’的信息
db.students.remove({"$remove":/大/},true);

//游标,指的是数据可以一行行的进行操作,类似于ResultSet
//只需要使用find()函数,就可以返回游标了
var cursor=db.students.find();
cursor.hasNext();//判断是否有下一行数据
cursor.next();//取出当前数据
//例:
var cursor=db.students.find();
while(cursor.hasNext()){
var doc=cursor.next();
//print(doc.name);
printjson(doc);
}

//索引
//查看默认状态下的students集合的索引内容
db.students.getIndexes();
//索引创建:db.集合.ensureIndex({列:1});
//----设置1表示索引按照升序方式排列,-1表示降序排列
db.students.ensureIndex({"age":-1});
db.students.find({"age":22}).explain();
db.students.find({"score":89}).explain();
db.students.find({"score":{"$eq":89}}).explain();
//复合索引,不会主动调用,强制调用使用hint();
db.students.ensureIndex({"age":-1,"score":-1},{"name":"age_-1_score_-1_index"});
db.students.find({"age":22,"score":89}).hint({"age":-1,"score":-1}).explain();
//删除一个索引
db.students.dropIndex({"age":-1,"score":-1});
//删除全部索引,删除所有非"_id"的索引,删除自定义的索引
db.students.dropIndexes();

//唯一索引:用在某一字段上,是该字段内容不重复
//name 字段内容不允许重复
db.students.ensureIndex({"name":1},{"unique":true});

//过期索引:在一些程序站点会出现若干秒之后信息被删除的情况
//设置过期索引
db.phones.ensureIndex({"time":1},{expireAfterSeconds:10});
show databases;
//在一个iphone集合里面设置过期索引,还需要保存时间信息
db.phones.insert({"tel":"110","code":"110","time":new Date()});
db.phones.insert({"tel":"111","code":"111","time":new Date()});
db.phones.insert({"tel":"112","code":"112","time":new Date()});
db.phones.insert({"tel":"113","code":"113","time":new Date()});
db.phones.insert({"tel":"114","code":"114","time":new Date()});
db.phones.insert({"tel":"115","code":"115","time":new Date()});

//全文索引
db.news.insert({"title":"mldn","content":"lxh"});
db.news.insert({"title":"pkpk","content":"pkq"});
db.news.insert({"title":"zzzz","content":"but"});
db.news.insert({"title":"but","content":"nef"});
//设置全文检索
db.news.ensureIndex({"title":"text","content":"text"});
//实现数据的模糊查询
//----要表示出全文检索,则使用“$text”判断符,进行数据查询则使用“$search”
//------查询指定关键字:{"$search":"查询关键字"}
//------查询多个关键字(或关系){"$search":"查询关键字 查询关键字...."}
//------查询多个关键字(或关系){"$search":"\"查询关键字\" \"查询关键字\"...."}
//------查询多个关键字(或关系){"$search":"查询关键字 查询关键字....-排除关键字"}
//单个内容查询
db.news.find({"$text":{"$search":"but"}});

//地理信息索引,保存的是坐标,经纬度
//--2D平面索引
//--2DSphere球面索引
//定义商铺集合
db.shop.insert({loc:[10,10]});
db.shop.insert({loc:[10,11]});
db.shop.insert({loc:[11,10]});
db.shop.insert({loc:[12,19]});
db.shop.insert({loc:[15,12]});
db.shop.insert({loc:[40,110]});
//为集合定义2D索引
db.shop.ensureIndex({"loc":"2d"});
//"$near",查询,查询距离某个点最近的坐标
//"$geoWithin",查询,查询某个形状内的点
//假设我现在的点坐标是:{11,11}
db.shop.find({loc:{"$near":[11,11]}});
//查询距离范围,2D平面索引里面支持最大距离,不支持最小距离
db.shop.find({loc:{"$near":[11,11],"$maxDistance":5}});
//矩形范围
($box):{"$box":[[x1,y1],[x2,y2]]}
db.shop.find({loc:{"$geoWithin":{"$box":[[9,9],[11,11]]}}});
//圆形范围
($center):{"$center":[[x1,y1],r]};
db.shop.find(loc:{"$geoWithin":{"$center":[[10,10],2]}});
//多边形
($polygon):{"$polygon":[x1,y1],[[x2,y2],[x3,y3]...]};
//利用runCommand()实现信息查询
db.runCommand({"geoNear":"shop",near:[10,10],maxDistance:5,num:2});;

MongoDB学习笔记-1的更多相关文章

  1. MongoDB学习笔记系列

    回到占占推荐博客索引 该来的总会来的,Ef,Redis,MVC甚至Sqlserver都有了自己的系列,MongoDB没有理由不去整理一下,这个系列都是平时在项目开发时总结出来的,希望可以为各位一些帮助 ...

  2. PHP操作MongoDB学习笔记

    <?php/*** PHP操作MongoDB学习笔记*///*************************//**   连接MongoDB数据库  **////*************** ...

  3. MongoDB 学习笔记(原创)

    MongoDB 学习笔记 mongodb 数据库 nosql 一.数据库的基本概念及操作 SQL术语/概念 MongoDB术语/概念 解释/说明 database database 数据库 table ...

  4. mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...

  5. MongoDB学习笔记(转)

    MongoDB学习笔记(一) MongoDB介绍及安装MongoDB学习笔记(二) 通过samus驱动实现基本数据操作MongoDB学习笔记(三) 在MVC模式下通过Jqgrid表格操作MongoDB ...

  6. 【转】MongoDB学习笔记(查询)

    原文地址 MongoDB学习笔记(查询) 基本查询: 构造查询数据. > db.test.findOne() { "_id" : ObjectId("4fd58ec ...

  7. MongoDB学习笔记(六)--复制集+sharding分片 && 总结

    复制集+sharding分片                                                               背景 主机 IP 服务及端口 Server A ...

  8. MongoDB学习笔记(五)--复制集 && sharding分片

    主从复制                                                                                       主从节点开启 主节 ...

  9. MongoDB学习笔记(四)--索引 && 性能优化

    索引                                                                                             基础索引 ...

  10. MongoDB学习笔记(三)--权限 && 导出导入备份恢复 && fsync和锁

    权限                                                                                             绑定内网I ...

随机推荐

  1. tensorflow的一些基础用法

    TensorFlow是一个采用数据流图,用于数值计算的开源软件库.自己接触tensorflow比较的早,可是并没有系统深入的学习过,现在TF在深度学习已经成了"标配",所以打算系统 ...

  2. 【BZOJ1055】[HAOI2008]玩具取名(动态规划)

    [BZOJ1055][HAOI2008]玩具取名(动态规划) 题面 BZOJ 洛谷 题解 裸的区间\(dp\),设\(f[i][j][W/I/N/G]\)表示区间\([i,j]\)能否由某个字母替换过 ...

  3. KEIL5.25生成.bin文件步骤

    添加.bin文件转换工具 KEIL5的自带.bin文件转化工具在安装目录下:我的安装目录是C盘即,C:\Keil_v5\ARM\ARMCC\bin\fromelf.exe 添加格式为:[C:\Keil ...

  4. 前端学习 -- Css -- 字体

    设置字体颜色,使用color来设置文字的颜色 设置文字的大小,浏览器中一般默认的文字大小都是16pxfont-size设置的并不是文字本身的大小,在页面中,每个文字都是处在一个看不见的框中的我们设置的 ...

  5. Codeforces Round #541

    因为这次难得不在十点半(或是更晚),大家都在打,然后我又双叒叕垫底了=.= 自己对时间的分配,做题的方法和心态还是太蒻了,写的时候经常写一半推倒重来.还有也许不是自己写不出来,而是在开始写之前就觉得自 ...

  6. 【CH4201】楼兰图腾

    题目大意:给定一个长度为 N 的序列,从序列中任意挑出三个数,求满足中间的数字值最小(最大)有多少种情况. 题解:建立在值域上的树状数组,从左到右扫描一遍序列,统计出每个点左边有多少个数大于(小于)该 ...

  7. Windows完成端口与Linux epoll技术简介

    收藏自:http://www.cnblogs.com/cr0-3/archive/2011/09/09/2172280.html WINDOWS完成端口编程1.基本概念2.WINDOWS完成端口的特点 ...

  8. pi的求法 acos(-1.0)

    pi=acos(-1.0) https://www.luogu.org/problemnew/show/T4529 #include <cstdio> #include <cstdl ...

  9. 稳定排序nlogn之归并排序_一维,二维

    稳定排序nlogn之归并排序_一维,二维 稳定排序:排序时间稳定的排序 稳定排序包括:归并排序(nlogn),基数排序[设待排序列为n个记录,d个关键码,关键码的取值范围为radix,则进行链式基数排 ...

  10. Eclipse Neon安装指导

    [下载] 前往Eclipse官网:http://www.eclipse.org/,点击DOWNLOAD: 进入下载页面后,会显示如下下载界面: 找到 Get Eclipse Neon,然后点击下面的” ...