|
> use stady //使用库 switched to db stady
> db.createCollection('school') //创建school集合
{ "ok" : 1 }
> db.school.insert({"id":1,"name":"lisi","score":90}) //向集合中插入数据
WriteResult({ "nInserted" : 1 })
> db.school.find() //查看集合中数据
{ "_id" : ObjectId("5b4843900edf47ef3aa006f3"), "id" : 1, "name" : "lisi", "score" : 90 }
> for(var i=2;i db.school.find() //查看集合数据
{ "_id" : ObjectId("5b4843900edf47ef3aa006f3"), "id" : 1, "name" : "lisi", "score" : 90 }
{ "_id" : ObjectId("5b4844e70edf47ef3aa006f4"), "id" : 2, "name" : "tom2" }
{ "_id" : ObjectId("5b4844e70edf47ef3aa006f5"), "id" : 3, "name" : "tom3" }
{ "_id" : ObjectId("5b4844e70edf47ef3aa006f6"), "id" : 4, "name" : "tom4" }
{ "_id" : ObjectId("5b4844e70edf47ef3aa006f7"), "id" : 5, "name" : "tom5" }
> db.school.findOne({"id":3}) //查看集合中第三条数据
{ "_id" : ObjectId("5b4844e70edf47ef3aa006f5"), "id" : 3, "name" : "tom3" }
> a=db.school.findOne({"id":3}) //查看第三条记录,并将其赋予别名a
{ "_id" : ObjectId("5b4844e70edf47ef3aa006f5"), "id" : 3, "name" : "tom3" }
> typeof(a.id) //查看属性类型
number //属性为数字
> typeof(a.name) //查看属性类型
string //属性为字符串
> db.school.update({"id":3},{$set:{"name":"jack"}}) //修改数据
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.school.findOne({"id":3})
{ "_id" : ObjectId("5b4844e70edf47ef3aa006f5"), "id" : 3, "name" : "jack" }
> db.school.count() //统计集合中有多少条记录
5
> db.createCollection('tea') //创建新集合
{ "ok" : 1 }
> show tables //查看库中集合
school
tea
> db.tea.drop() //删除集合
true
> show tables
school //集合已删除
> show dbs //查看库
admin 0.000GB
config 0.000GB
local 0.000GB
python 0.000GB
stady 0.000GB
> use python
switched to db python
> db.dropDatabase() //删除python数据库,首先必须先进数据库,在选择删除
{ "dropped" : "python", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
stady 0.000GB
>
|
|
|