首页 > 代码库 > mongodb数据操作(CRUD)

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=http://www.mamicode.com/{
        "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":["河南","山东"]}
})

 

mongodb数据操作(CRUD)