mongodb数据操作(CRUD)

服务器

浏览数:98

2019-9-8

AD:资源代下载服务

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

操作

> use hk
switched to db hk
> show collections
> db.info.insert({"url":"www.lepu.cn"})
WriteResult({ "nInserted" : 1 })
> 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" : 3,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "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=0;i<1000;i++){
        db.info.insert({"url":"url->"+i});
}

操作


> for(var i=0;i<1000;i++){
... db.info.insert({"url":"url->"+i});
... }
WriteResult({ "nInserted" : 1 })
> 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":0,"url":1})
> db.info.find({"url":"www.9888.cn"},{"_id":0,"url":1})
{ "url" : "www.9888.cn" }

格式化显示
pretty()

var data={
        "name":"hkui",
        "age":26,
        "hobby":"footbal,music",
        "info":"it码农,哈哈哈哈哈哈哈"
};
db.info.insert(data);


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

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

构造查询数据


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

逻辑运算

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

取模

$mod
{$mod:[数字,余数]}
取出除数是20,,余数为1的
db.stu.find({"age":{"$mod":[20,1]}}).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" ] }

 

作者:H&K