1. mongodb创建数据库和集合 mongodb数据库和集合的创建是隐式的。意思是说不用单独写create database 语句。直接使用use关键词即可。在bin/mongo shell下运行: use test; 这样会生成test数据库,如果不写入离开,系统自动删掉。集合也是隐式的,不用专门指
use test;
db.user.insert({"name" : "gang"});
db.user.remove();
db.user.remove({"name" : "gang"});
db.user.update({"name" : "gang"}, {"new_name" : "gang"});
db.user.find(); { "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "new_name" : "gang" }
db.user.update({"name" : "gang"}, {"$set" : {"age" : 25}});
db.user.find(); { "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "age" : 25, "name" : "gang" }
db.user.update({"name" : "gang"}, {"$unset" : {"age" : 1}});
db.user.find(); { "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "name" : "gang" }
db.user.update({"name" : "gang"}, {"$inc" : {"score" : 5}});
db.user.update({"name" : "gang"}, {"$inc" : {"score" : -5}});
db.user.update({"name" : "gang"}, {"$push" : {"subjects" : {"chinese" : 10, "math" : 15}}});
>db.user.find(); { "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "name" : "gang", "age" : [ 1, 2, 3, 4 ] } >db.user.update({"name" : "gang"}, {"$pop" : {"age" : 1}}); >db.user.find(); { "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "age" : [ 1, 2, 3 ], "name" : "gang" } > db.user.update({"name" : "gang"}, {"$pop" : {"age" : -1}}); > db.user.find(); { "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "age" : [ 2, 3 ], "name" : "gang" }
> db.user.update({"name" : "gang"}, {"$pull" : {"age" : 2}}); > db.user.find(); { "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "age" : [ 3 ], "name" : "gang" }