mongodb数据操作(CRUD)

Posted 梦想远航

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb数据操作(CRUD)相关的知识,希望对你有一定的参考价值。

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" }
View Code

批量插入

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显示下一页
View Code

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码农,哈哈哈哈哈哈哈"
}
View Code

关系查询
大于($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":"美国"});
View Code
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]});
View Code

 

查询出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代码语法结构,这种方式不方便使用数据库索引机制


















以上是关于mongodb数据操作(CRUD)的主要内容,如果未能解决你的问题,请参考以下文章

为 MongoDB 上的 CRUD 操作组织 Go 代码

MongoDB学习笔记-使用 MongoDB 进行 CRUD 操作(上)

MongoDB学习笔记-使用 MongoDB 进行 CRUD 操作(下)

使用TS封装操作MongoDB数据库的工具方法

mongodb数据操作(CRUD)

mongodb数据操作(CRUD)