|
## 创建数据库 ,不存在会创建,不建立集合又会删除 > use mydb;
switched to db mydb
##创建集合
> db.createCollection('a')
{ "ok" : 1 }
## 在集合中插入数据
> db.a.insert({"id":1,"name":"zhangsan"})
WriteResult({ "nInserted" : 1 })
## 查看集合中的数据
> db.a.find()
{ "_id" : ObjectId("5b4c54bc8a4352592ecc288f"), "id" : 1, "name" : "zhangsan" }
##查找指定记录并赋予别名a,查看属性类型
> b=db.a.findOne({"id":1})
{
"_id" : ObjectId("5b4c54bc8a4352592ecc288f"),
"id" : 1,
"name" : "zhangsan"
}
> typeof(b.id)
number
##更改数据
> db.a.update({"id":1},{$set:{"name":"tom"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.a.find()
{ "_id" : ObjectId("5b4c54bc8a4352592ecc288f"), "id" : 1, "name" : "tom" }
##查看集合
> show collections
a
##删除集合
> db.a.drop()
true
##删除数据库
> db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
##复制数据库
> db.copyDatabase("mydb","mydb1")
{ "ok" : 1 }
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
mydb 0.000GB
mydb1 0.000GB
|
|
|