$ mongo //连接进数据库后



> show dbs; //显示所有database



> show foobar; //选择某个数据库



> show collections; //显示所有collections;



> var user = {"name": "may", "age": 23, "school": "TJUT"}; //创建一个user对象,设定相关属性



> db.users.insert(user); //如果不存在则创建一个users collection,并且插入了这个user



> db.users.insert({"name": "may", "age": 23, "school": "TJUT"}); //也可这么插入



> db.user.save(user);



> db.users.save({"name": "may", "age": 23, "school": "TJUT"});



> db.users.find(); //查找users collection 中的任意数据



> db.users.findOne(); //查找一个数据,

//上述find没有增加限定条件



> db.users.find({"name": "may"}); //在users collection中搜索name为may的所有的文档



> db.users.findOne({"name": "may"}); //在users collection 中搜索一个name为may的文档



//某个实例

> db.users.find();



{ "_id" : ObjectId("4fcf60accb7539fbf4ecb9b1"), "name" : "may", "age" : 1, "school" : "TJUT" }
{ "_id" : ObjectId("4fcf60d1cb7539fbf4ecb9b2"), "name" : "kai", "age" : 24, "school" : "ysu" }
{ "_id" : ObjectId("4fcf60e4cb7539fbf4ecb9b3"), "name" : "may" }

> db.users.find({"name": "may"});

{ "_id" : ObjectId("4fcf60accb7539fbf4ecb9b1"), "name" : "may", "age" : 1, "school" : "TJUT" }
{ "_id" : ObjectId("4fcf60e4cb7539fbf4ecb9b3"), "name" : "may" }

> db.users.findOne({"name": "may"});

{
"_id" : ObjectId("4fcf60accb7539fbf4ecb9b1"),
"name" : "may",
"age" : 1,
"school" : "TJUT"
}

> db.users.remove({"age": 1}); //删除users中age为1的文档

> db.users.remove(); //清空这个users collection

> db.users.clean(); //清空这个users collection


//修改



首先,获取某个记录

> var may = db.users.findOne({"name": "may", "age": 1});

> may;



{ "_id" : ObjectId("4fcf6483cb7539fbf4ecb9b5"), "name" : "may", "age" : 1 }

> db.users.find();

{ "_id" : ObjectId("4fcf6483cb7539fbf4ecb9b5"), "name" : "may", "age" : 1 }
{ "_id" : ObjectId("4fcf65e5cb7539fbf4ecb9b6"), "name" : "may" }
{ "_id" : ObjectId("4fcf6634cb7539fbf4ecb9b7"), "name" : "may", "age" : "二十" }


> delete may._id;

> may.age = 20;

> db.users.update({"name": "may"}, may);

> db.users.find();



{ "_id" : ObjectId("4fcf6483cb7539fbf4ecb9b5"), "name" : "may", "age" : 20 }
{ "_id" : ObjectId("4fcf65e5cb7539fbf4ecb9b6"), "name" : "may" }
{ "_id" : ObjectId("4fcf6634cb7539fbf4ecb9b7"), "name" : "may", "age" : "二十" }

这样我们会把一个name为may的文档修改age为20




> db.users.update({"name": "may"}, {"name": "kai"}); //修改