|
> use mydb //创建并打开mydb数据库 switched to db mydb
> a=db.info.find() //起别名
> db.createCollection('a') //向mydb数据库添加集合a
{ "ok" : 1 }
> show collections //显示数据库中的所有集合
a
> typeof(a.id) //查看属性类型
2018-07-16T17:51:21.735+0800 E QUERY [thread1] ReferenceError: a is not defined :
@(shell):1:1
> db.a.insert({"id":1,"name":"zhangsan","hobby":["game","talk","read"]})
WriteResult({ "nInserted" : 1 })
["game","talk","read"] //字符串数组 object(对象)
> db.a.find() //显示a集合中的所有文档
{ "_id" : ObjectId("5b4c58ddf7c9ed9a709175e9"), "id" : 1, "name" : "zhangsan", "hobby" : [ "game", "talk", "read" ] }
> db.a.insert({"id":2,"name":"lisi","hobby":["game","talk","read"]})
WriteResult({ "nInserted" : 1 })
> db.a.insert({"id":3,"name":"wangwu","hobby":["game","talk","read"]})
WriteResult({ "nInserted" : 1 })
> db.a.find()
{ "_id" : ObjectId("5b4c58ddf7c9ed9a709175e9"), "id" : 1, "name" : "zhangsan", "hobby" : [ "game", "talk", "read" ] }
{ "_id" : ObjectId("5b4c67c5bacc9efbe3a0d477"), "id" : 2, "name" : "lisi", "hobby" : [ "game", "talk", "read" ] }
{ "_id" : ObjectId("5b4c67ccbacc9efbe3a0d478"), "id" : 3, "name" : "wangwu", "hobby" : [ "game", "talk", "read" ] }
> db.a.findOne({"id":1}) //查找指定记录
{
"_id" : ObjectId("5b4c58ddf7c9ed9a709175e9"),
"id" : 1,
"name" : "zhangsan",
"hobby" : [
"game",
"talk",
"read"
]
}
> db.a.update({"id":2},{$set:{"name":"xiaoqi"}}) //更改属性
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.a.find()
{ "_id" : ObjectId("5b4c58ddf7c9ed9a709175e9"), "id" : 1, "name" : "zhangsan", "hobby" : [ "game", "talk", "read" ] }
{ "_id" : ObjectId("5b4c67c5bacc9efbe3a0d477"), "id" : 2, "name" : "xiaoqi", "hobby" : [ "game", "talk", "read" ] }
{ "_id" : ObjectId("5b4c67ccbacc9efbe3a0d478"), "id" : 3, "name" : "wangwu", "hobby" : [ "game", "talk", "read" ] }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mydb 0.000GB
> use mydb
switched to db mydb
> db.a.drop() //删除集合
true
> show collections
> db.dropDatabase() //删除数据库
{ "dropped" : "mydb", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
|
|