大数据

NoSQL MongoDB数据库访问及操作

时间:2014-7-8 22:41:57  作者:solgle  来源:www.solgle.com  查看:359  评论:0
内容摘要:MongoDB数据库访问及操作1:创建数据库solgle-db[root@www.solgle.com bin]# ./mongo localhost:27017MongoDB shell version: 2.6.5connecting to: localhost:27017/...
MongoDB数据库访问及操作
本文出自:http://www.solgle.com/news/134.html
 
1:创建数据库solgle-db
[root@www.solgle.com bin]# ./mongo localhost:27017
MongoDB shell version: 2.6.5
connecting to: localhost:27017/test
> use solgle-db    ###没有则自动创建
switched to db solgle-db
>
2:插入数据
> db.solgle-db.save({name:"liudahua",address:"hanghong"});
WriteResult({ "nInserted" : 1 })
>
 
> user1={name:"liuruoyin",address:"aomen"};
{ "name" : "liuruoyin", "address" : "aomen" }
> db.solgle-db.save(user1);
WriteResult({ "nInserted" : 1 })
>
 
3:查询数据
> db.solgle-db.find();
{ "_id" : ObjectId("543798c2de8aef96291acf7d"), "name" : "liudahua", "address" : "hanghong" }
{ "_id" : ObjectId("5437992dde8aef96291acf7e"), "name" : "liuruoyin", "address" : "aomen" }
>
注:_id为主键,其具有唯一性
 
4:insert和save的使用
> db.solgle-db.insert({name:"yaomin",address:"beijing",desc:"basketball"});
WriteResult({ "nInserted" : 1 })
>
> db.solgle-db.save({name:"yaomin",address:"beijing",desc:"basketball"});
WriteResult({ "nInserted" : 1 })
>
> db.solgle-db.find();
{ "_id" : ObjectId("543798c2de8aef96291acf7d"), "name" : "liudahua", "address" : "hanghong" }
{ "_id" : ObjectId("5437992dde8aef96291acf7e"), "name" : "liuruoyin", "address" : "aomen" }
{ "_id" : ObjectId("54379aa1de8aef96291acf7f"), "name" : "yaomin", "address" : "beijing", "desc" : "basketball" }
{ "_id" : ObjectId("54379abade8aef96291acf80"), "name" : "yaomin", "address" : "beijing", "desc" : "basketball" }
>
 
5:更新数据
 
update(query,obj,upset,multi)
upset 默认为false,为true时如果不存在则添加记录
multi 默认为false,为true是更新所有符合条件的记录,否则只更新第一条符合条件的记录
eg:
> db.solgle-dbname.update({"name":"yaomin"},{$set:{"desc":"football"}},true/false,true/false);
>常用更新操作
> db.solgle-db.update({"name":"yaomin"},{"desc":"basketball star"});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.solgle-db.find();
{ "_id" : ObjectId("543798c2de8aef96291acf7d"), "name" : "liudahua", "address" : "hanghong" }
{ "_id" : ObjectId("5437992dde8aef96291acf7e"), "name" : "liuruoyin", "address" : "aomen" }
{ "_id" : ObjectId("54379aa1de8aef96291acf7f"), "desc" : "basketball star" }
{ "_id" : ObjectId("54379abade8aef96291acf80"), "name" : "yaomin", "address" : "beijing", "desc" : "basketball" }
>###可以看出修改了yaomin的第一条记录
 
> db.solgle-db.update({"name":"yaomin"},{$set:{"address":"beijing","desc":"basketball star"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.solgle-db.find();  
{ "_id" : ObjectId("543798c2de8aef96291acf7d"), "name" : "liudahua", "address" : "hanghong" }
{ "_id" : ObjectId("5437992dde8aef96291acf7e"), "name" : "liuruoyin", "address" : "aomen" }
{ "_id" : ObjectId("54379aa1de8aef96291acf7f"), "desc" : "basketball star" }
{ "_id" : ObjectId("54379abade8aef96291acf80"), "name" : "yaomin", "address" : "beijing", "desc" : "basketball star" }
>###可以看出修改了最后一条记录
 
 
> db.solgle-db.update({"name":"yaomin"},{$unset:{"desc":"basketball star2"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.solgle-db.find();
{ "_id" : ObjectId("543798c2de8aef96291acf7d"), "name" : "liudahua", "address" : "hanghong" }
{ "_id" : ObjectId("5437992dde8aef96291acf7e"), "name" : "liuruoyin", "address" : "aomen" }
{ "_id" : ObjectId("54379aa1de8aef96291acf7f"), "desc" : "basketball star" }
{ "_id" : ObjectId("54379abade8aef96291acf80"), "name" : "yaomin", "address" : "beijing" }
>###desc被去掉
 
> db.solgle-db.update({"name":"yaomin"},{$unset:{"address":1}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.solgle-db.find();
{ "_id" : ObjectId("543798c2de8aef96291acf7d"), "name" : "liudahua", "address" : "hanghong" }
{ "_id" : ObjectId("5437992dde8aef96291acf7e"), "name" : "liuruoyin", "address" : "aomen" }
{ "_id" : ObjectId("54379aa1de8aef96291acf7f"), "desc" : "basketball star" }
{ "_id" : ObjectId("54379abade8aef96291acf80"), "name" : "yaomin" }
>###address被去掉
 
这里比较了$set与$unset的区别
 
 
6:删除数据
 
> db.solgle-db.remove({"name":"liudehua2"});  ###删除name为liudehua2的记录
WriteResult({ "nRemoved" : 0 })
>
###删除所有数据
> db.solgle-db.remove();
>
 
 
标签:MongoDB NoSQL数据库访问及操作 

solgle.com 版权所有,欢迎分享!!!

相关评论
   Copyright © 2013-2020 solgle.com,All rights reserved.[solgle.com] 公安机关备案号:51010802000219
Email:solgle@solgle.com; weixin:cd1008610000 ICP:蜀ICP备14011070号-1