1.数据插入
db.集合名.insert()

操作

> use hk
switched to db hk
> show collections
> db.info.insert({"url":"www.lepu.cn"})
WriteResult({ "nInserted" : })
> db.info.find()
{ "_id" : ObjectId("592bd4324c964b7784cca90f"), "url" : "www.lepu.cn" }

数组插入

db.info.insert([
{"url":"www.baidu.com"},
{"url":"www.9888.cn"},
{"url":"www.a.cn"}
]);

操作

> db.info.insert([
... {"url":"www.baidu.com"},
... {"url":"www.9888.cn"},
... {"url":"www.a.cn"}
... ]);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : ,
"nUpserted" : ,
"nMatched" : ,
"nModified" : ,
"nRemoved" : ,
"upserted" : [ ]
})
> db.info.find()
{ "_id" : ObjectId("592bd4324c964b7784cca90f"), "url" : "www.lepu.cn" }
{ "_id" : ObjectId("592bd4f84c964b7784cca910"), "url" : "www.baidu.com" }
{ "_id" : ObjectId("592bd4f84c964b7784cca911"), "url" : "www.9888.cn" }
{ "_id" : ObjectId("592bd4f84c964b7784cca912"), "url" : "www.a.cn" }

批量插入

for(var i=;i<;i++){
db.info.insert({"url":"url->"+i});
}

操作

> for(var i=;i<;i++){
... db.info.insert({"url":"url->"+i});
... }
WriteResult({ "nInserted" : })
> db.info.find()
{ "_id" : ObjectId("592bd4324c964b7784cca90f"), "url" : "www.lepu.cn" }
{ "_id" : ObjectId("592bd4f84c964b7784cca910"), "url" : "www.baidu.com" }
{ "_id" : ObjectId("592bd4f84c964b7784cca911"), "url" : "www.9888.cn" }
{ "_id" : ObjectId("592bd4f84c964b7784cca912"), "url" : "www.a.cn" }
{ "_id" : ObjectId("592bd5bf4c964b7784cca913"), "url" : "url->0" }
{ "_id" : ObjectId("592bd5bf4c964b7784cca914"), "url" : "url->1" }
{ "_id" : ObjectId("592bd5bf4c964b7784cca915"), "url" : "url->2" }
.......................
{ "_id" : ObjectId("592bd5bf4c964b7784cca921"), "url" : "url->14" }
{ "_id" : ObjectId("592bd5bf4c964b7784cca922"), "url" : "url->15" }
Type "it" for more
在数据保存很多的情况下,只会显示20条,按it显示下一页

2.数据查询

关系运算,逻辑运算,数组运算,正则运算等
db.集合名称.find({查询条件},[{显示的字段}])
查询出url为"www.9888.cn"的数据
db.info.find({"url":"www.9888.cn"});
不需要显示的字段设为0,默认都是1

db.info.find({"url":"www.9888.cn"},{"_id":,"url":})
> db.info.find({"url":"www.9888.cn"},{"_id":,"url":})
{ "url" : "www.9888.cn" }

格式化显示
pretty()

var data={
"name":"hkui",
"age":,
"hobby":"footbal,music",
"info":"it码农,哈哈哈哈哈哈哈"
};
db.info.insert(data);
> db.info.find({"name":"hkui"})
{ "_id" : ObjectId("592bda084c964b7784ccacfb"), "name" : "hkui", "age" : , "hobby" : "footbal,music", "info" : "it码农,哈哈哈哈哈哈哈" }
> db.info.find({"name":"hkui"}).pretty()
{
"_id" : ObjectId("592bda084c964b7784ccacfb"),
"name" : "hkui",
"age" : ,
"hobby" : "footbal,music",
"info" : "it码农,哈哈哈哈哈哈哈"
}

关系查询
大于($gt)
小于($lt)
小于等于($lte)
大于等于($gte)
不等于($ne)

构造查询数据

db.stu.drop();
db.stu.insert({"name":"张三","sex":"男","age":,"score":,"address":"河南"});
db.stu.insert({"name":"李四","sex":"女","age":,"score":,"address":"山东"});
db.stu.insert({"name":"王五","sex":"男","age":,"score":,"address":"江苏"});
db.stu.insert({"name":"赵六","sex":"男","age":,"score":,"address":"山东"});
db.stu.insert({"name":"孙七","sex":"女","age":,"score":,"address":"湖北"});
db.stu.insert({"name":"tom","sex":"男","age":,"score":,"address":"海南"});
db.stu.insert({"name":"lucy","sex":"女","age":,"score":,"address":"浙江"});
db.stu.insert({"name":"jack","sex":"男","age":,"score":,"address":"美国"});
db.stu.insert({"name":"smith","sex":"男","age":,"score":,"address":"美国"});
db.stu.find({"name":"tom"}).pretty();
db.stu.find({"address":"山东"}).pretty();
age大于20的
db.stu.find({"age":{"$gt":}}).pretty();
查询姓名不是tom的
db.stu.find({"name":{"$ne":"tom"}}).pretty();

逻辑运算

与($and)
或($or)
非($not,$nor)
查询年龄在18~
db.stu.find({"age":{"$gte":,"$lte":}})
查询年龄小于20或者score>80的
db.stu.find({"$or":
[
{"age":{"$lt":}},
{"score":{"$gt":}}
]
}).pretty();
取反操作
db.stu.find({"$nor":
[
{"age":{"$lt":}},
{"score":{"$gt":}}
]
}).pretty();

取模

$mod
{$mod:[数字,余数]}
取出除数是20,,余数为1的
db.stu.find({"age":{"$mod":[,]}}).pretty();
范围查询
$in或者$nin
db.stu.find({
"address":{"$in":["河南","山东"]}
})

 数组查询

针对数组内容,运算符有

$all,$size,$slice,$elemMath

构造数据

db.stus.drop();
db.stus.insert({"name":"hk1","sex":"男","age":18,"score":70,"address":"河南","course_id":[1,2,3,4,5]});
db.stus.insert({"name":"hk2","sex":"女","age":20,"score":60,"address":"山东","course_id":[1,2,3,4]});
db.stus.insert({"name":"hk3","sex":"男","age":17,"score":44,"address":"江苏","course_id":[1,2,3]});
db.stus.insert({"name":"hk4","sex":"男","age":21,"score":80,"address":"山东","course_id":[1,3]});
db.stus.insert({"name":"hk5","sex":"女","age":23,"score":50,"address":"湖北","course_id":[2,3,4]});
db.stus.insert({"name":"tom","sex":"男","age":24,"score":20,"address":"海南","course_id":[1]});
db.stus.insert({"name":"lucy","sex":"女","age":21,"score":62,"address":"浙江","course_id":[3,4]});
db.stus.insert({"name":"jack","sex":"男","age":20,"score":90,"address":"美国","course_id":[1,2,3,4]});
db.stus.insert({"name":"smith","sex":"男","age":19,"score":88,"address":"美国","course_id":[2,4]});

查询同事参加 2,4课程的

db.stus.find({"course_id":{"$all":[2,4]}}).pretty();
db.stus.find({"address":{"$all":["河南","山东"]}}).pretty();
//无结果
db.stus.find({"address":{"$all":["河南"]}}).pretty();
//查出了家是河南的

索引操作(从0 开始)
第二个为4的

db.stus.find({"course_id.1":4}).pretty();
//只参加2门课程的
db.stus.find({"course_id":{"$size":2}}).pretty();
//数组截取
//取前2门
db.stus.find({"age":18},{"course_id":{"$slice":2}}).pretty(); //1,2
//后2门
db.stus.find({"age":18},{"course_id":{"$slice":-2}}).pretty();//4,5 db.stus.find({"age":18},{"course_id":{"$slice":[1,2]}}).pretty();//2,3 db.stus.find({"age":18},{"course_id":{"$slice":[3,1]}}).pretty();//4 //闭合区间,长度

 $elemMatch

数据

db.stus1.drop();
db.stus1.insert({"name":"hk1","sex":"男","age":18,"score":70,"address":"河南","course_id":[1,2,3,4,5],
"parents":[
{"name":"hk1_father","job":"程序员","age":50},
{"name":"hk1_mother","job":"工人","age":45} ]
});
db.stus1.insert({"name":"hk2","sex":"女","age":20,"score":60,"address":"山东","course_id":[1,2,3,4],
"parents":[
{"name":"hk2_father","job":"教师","age":52},
{"name":"hk2_mother","job":"程序员","age":45} ]
});
db.stus1.insert({"name":"hk3","sex":"男","age":17,"score":44,"address":"江苏","course_id":[1,2,3],
"parents":[
{"name":"hk3_father","job":"教授","age":45},
{"name":"hk3_mother","job":"销售","age":40} ]
});
db.stus1.insert({"name":"hk4","sex":"男","age":21,"score":80,"address":"山东","course_id":[1,3]});
查询出age大于18,父母里有是程序员的
db.stus1.find({
"$and":
[
{ "age":{"$gt":18}},
{ "parents":{ "$elemMatch":
{"job":"程序员"}
}
}
]
}).pretty(); 查询出age大于18,父母里有是程序员的而且程序员年龄小于50
db.stus1.find({
"$and":
[
{ "age":{"$gt":18}},
{ "parents":{ "$elemMatch":
{"$and":
[{"job":"程序员"},{"age":{"$lt":50}}]
}
}
}
]
}).pretty();

判断某个字段是否存在 $exists
true表示存在,false表示不存在
判断没有父母信息存在的

db.stus.find({"parents":{"$exists":true}}).pretty();
db.stus.find({"parents":{"$exists":false}}).pretty();

条件过滤
$where

查询年龄大于20的
db.stu.find({"$where":"this.age>20"});
db.stu.find("this.age>20");

每一行进行判断
或者

db.stu.find(function(){
return this.age>20;
})

或者

db.stu.find({"$where":function(){
return this.age>20;
}});

复合条件判断

db.stu.find({"$and":[
{"$where":"this.age>20"},
{"$where":"this.age<22"}, ]});

缺点把bson变成js代码语法结构,这种方式不方便使用数据库索引机制

模糊查询正则运算

mongo里实现模糊查询必须使用正则

基础语法:{key:正则标记}
完整语法: {key:{"$regex":正则标记,"$options":'模式修正符'}}

模式修正符 :
i 忽略大小写
m 多行查找
x 忽略空白字符串
s 所有字符 包括换行
如果直接使用javascript,只能使用i,m,使用x或者s必须使用$regex
使用i,m时可以使用不用$regex的简写

演示

var data=[
{"lan":"php7","need":["mysql",'js','html','css','tp','yii']},
{"lan":"jav a","need":["mysql",'js','html','css','oracle','spring']},
{"lan":"python","need":["mysql",'js','html','css','flask']},
{"lan":"PythoN","need":["mysql",'js','html','css','flask','diagno']}
]; db.web.insert(data); db.web.find({"lan":/python/});
{ "_id" : ObjectId("592c945f997b7830e7f76d07"), "lan" : "python", "need" : [ "mysql", "js", "html", "css", "flask" ] }
db.web.find({"lan":/python/i});
{ "_id" : ObjectId("592c945f997b7830e7f76d07"), "lan" : "python", "need" : [ "mysql", "js", "html", "css", "flask" ] }
{ "_id" : ObjectId("592c945f997b7830e7f76d08"), "lan" : "PythoN", "need" : [ "mysql", "js", "html", "css", "flask", "diagno" ] }
db.web.find({"lan":{"$regex":/python/,"$options":'i'}});
结果同 db.web.find({"lan":/python/i}); db.web.find({"lan":{"$regex":/p y t h on/,"$options":"xi"}})
{ "_id" : ObjectId("592c945f997b7830e7f76d07"), "lan" : "python", "need" : [ "mysql", "js", "html", "css", "flask" ] }
{ "_id" : ObjectId("592c945f997b7830e7f76d08"), "lan" : "PythoN", "need" : [ "mysql", "js", "html", "css", "flask", "diagno" ] } db.web.find({"lan":{"$regex":/jav\s*a/}})
{ "_id" : ObjectId("592c945f997b7830e7f76d06"), "lan" : "jav a", "need" : [ "mysql", "js", "html", "css", "oracle", "spring" ] }
//数字结尾
db.web.find({"lan":{"$regex":/\d$/}});
{ "_id" : ObjectId("592c945f997b7830e7f76d05"), "lan" : "php7", "need" : [ "mysql", "js", "html", "css", "tp", "yii" ] }
//大写字母开头
db.web.find({"lan":{"$regex":/^[A-Z]/}});
{ "_id" : ObjectId("592c945f997b7830e7f76d08"), "lan" : "PythoN", "need" : [ "mysql", "js", "html", "css", "flask", "diagno" ] }
//含有flask
db.web.find({"need":{"$regex":/flask/}});
{ "_id" : ObjectId("592c945f997b7830e7f76d07"), "lan" : "python", "need" : [ "mysql", "js", "html", "css", "flask" ] }
{ "_id" : ObjectId("592c945f997b7830e7f76d08"), "lan" : "PythoN", "need" : [ "mysql", "js", "html", "css", "flask", "diagno" ] }
//含有flask或者yii的
db.web.find({"need":{"$regex":/flask|yii/}});
"_id" : ObjectId("592c945f997b7830e7f76d05"), "lan" : "php7", "need" : [ "mysql", "js", "html", "css", "tp", "yii" ] }
{ "_id" : ObjectId("592c945f997b7830e7f76d07"), "lan" : "python", "need" : [ "mysql", "js", "html", "css", "flask" ] }
{ "_id" : ObjectId("592c945f997b7830e7f76d08"), "lan" : "PythoN", "need" : [ "mysql", "js", "html", "css", "flask", "diagno" ] }

mongodb数据操作(CRUD)的更多相关文章

  1. mongodb 数据操作CRUD

    链接到mongo 新建超级用户 上文中我们提到mongo用户库表管理.为了方便我们先新建一个root权限的用户. db.createUser({user:'dbadmin',pwd:'123456', ...

  2. Atitti.数据操作crud js sdk dataServiceV3设计说明

    Atitti.数据操作crud js sdk dataServiceV3设计说明 1. 增加数据1 1.1. 参数哦说明1 2. 查询数据1 2.1. 参数说明2 3. 更新数据2 3.1. 参数说明 ...

  3. ADO.NET访问Access(文本数据库)数据操作(CRUD)

    1,ADO.NET访问Access(文本数据库)数据操作(CRUD) 2,DatabaseDesign 文本数据库Northwind.mdb 3,/App_Code 3.1,/App_Code/DBC ...

  4. 笔记-mongodb数据操作

    笔记-mongodb数据操作 1.      数据操作 1.1.    插入 db.COLLECTION_NAME.insert(document) 案例: db.inventory.insertOn ...

  5. EasyUI-datagrid数据展示+MongoDB数据操作

    使用EasyUI-datagrid进行数据展示:进行添加,修改,删除操作逻辑代码,数据源来自MongoDB. 一.新建SiteInfo控制器,添加Index页面:http://www.cnblogs. ...

  6. MongoDB数据操作之删除与游标处理

    删除数据(比较常用) 范例:清空infos集合中的内容.表.文档.成员. db.infos.remove({"url":/-/}); 默认情况下都删除,第二个条件设为true,只删 ...

  7. SQL Server温故系列(1):SQL 数据操作 CRUD 之增删改合

    1.插入语句 INSERT INTO 1.1.用 INSERT 插入单行数据 1.2.用 INSERT 插入多行数据 1.3.用 INSERT 插入子查询结果行 1.4.INSERT 小结及特殊字段插 ...

  8. SQL Server温故系列(2):SQL 数据操作 CRUD 之简单查询

    1.查询语句 SELECT 1.1.查询语句的 SELECT 子句 1.2.查询语句的 FROM 子句 1.2.1.内连接查询 INNER JOIN 1.2.2.外连接查询 OUTER JOIN 1. ...

  9. Mongodb数据操作基础

    var mongodb = require('mongodb'); var server = new mongodb.Server('localhost', 27017, {auto_reconnec ...

随机推荐

  1. nginx 中location和root、alias

    nginx指定文件路径有两种方式root和alias,这两者的用法区别 root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件 ...

  2. 简单Trace类实现

    <C++沉思录>27章内容修改后所得: /************************************************************************/ ...

  3. GO学习笔记:函数Panic和Recover

    Go没有像Java那样的异常机制,它不能抛出异常,而是使用了panic和recover机制.一定要记住,你应当把它作为最后的手段来使用,也就是说,你的代码中应当没有,或者很少有panic的东西.这是个 ...

  4. python 图像库PIL详解

    PIL详细文档 The most important class in the Python Imaging Library is the Image class, defined in the mo ...

  5. Selenium with Python 005 - 调用js操作页面元素

    WebDriver提供了execute_script()方法来执行JavaScript方法,格式如 driver.execute_script(script,*args) 执行js一般有两种场景,一是 ...

  6. 【Java】对象的创建过程

    一.对象的创建过程 1.首次创建对象时或该类静态方法/静态域首次被访问时,JAVA解释器查找该类的路径,定位该类的class文件 2.载入该类的class文件,有关静态初始化的所有动作执行,但是只执行 ...

  7. New Concept English three (28)

    27w/m 87 Small boats loaded with wares sped to the great liner as she was entering the harbour. Befo ...

  8. Android 系统信息的获取

    Android 系统信息的获取 一.内存(ram): 1.android 的内存大小信息存放在系统的 /proc/meminfo 文件里面,通过adb shell 进入 手机目录后,使用 cat /p ...

  9. 前端画图之iphoneSE主屏

    今天逛园子,无意间看到一个用div+css画的Macbook Air的博客,瞬间想到很久之前我也做过类似的事, 而且,当时写完之后,真的是成就感爆棚啊!我去开源中国上翻到了我当时贴的源码,当时是在手机 ...

  10. Android 自定义组件之如何实现自定义组件

    参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699 简介 Android提供了用于构建UI的强大的组件模型.两个基类:View和 ...