MongoDB使用

登录MongoDB
 
[root@oracle11g ~]# mongo
 
MongoDB shell version: 2.4.9
connecting to: test
 
> use admin
 
switched to db admin
 
> show dbs
 
admin   (empty)
local   0.078125GB
 
 
使用新的数据库my_mongodb
 
> use my_mongodb
 
switched to db my_mongodb
 
 
插入三条数据
 
> db.user.insert({uid:1,username:"Tom",age:25});
> db.user.insert({uid:2,username:"Jerry",age:25});
> db.user.insert({uid:1,username:"oracle",age:30});
 
向数据库my_mongodb的表user中插入了2条记录。MongoDB会隐式的创建数据库my_mongodb和表user
 
 
查看当前数据库
 
> show dbs
 
admin   (empty)
local   0.078125GB
my_mongodb      0.203125GB
 
 
查看当前连接
 
> show collections
 
system.indexes
user
 
 
查看user表中的数据
 
> db.user.find();
 
{ "_id" : ObjectId("55823677108162d31a1af236"), "uid" : 1, "username" : "Tom", "age" : 25 }
{ "_id" : ObjectId("55823696108162d31a1af237"), "uid" : 2, "username" : "Jerry", "age" : 25 }
{ "_id" : ObjectId("558236c0108162d31a1af238"), "uid" : 1, "username" : "oracle", "age" : 30 }
 
 
查找usernameJerry的数据
 
> db.user.find({username:"Jerry"});
 
{ "_id" : ObjectId("55823696108162d31a1af237"), "uid" : 2, "username" : "Jerry", "age" : 25 }
 
 
更新uid2age 100
 
> db.user.update({uid:2},{$set:{age:100}}) ;
 
 
检查数据是否更新成功
 
> db.user.find({uid:2});
 
{ "_id" : ObjectId("55823696108162d31a1af237"), "uid" : 2, "username" : "Jerry", "age" : 100 }
 
posted @ 2018-10-19 09:32  Oracle-fans  阅读(388)  评论(0编辑  收藏  举报