增
- 增加单篇文档
> db.stu.insert({sn:'001', name:'lisi'})
WriteResult({ "nInserted" : 1 })
> db.stu.find()
{ "_id" : ObjectId("572f4fcf3c8cf279cf89ad19"), "sn" : "001", "name" : "lisi" }
>
- 增加单篇文档并指定id
> db.stu.insert({_id:2, sn:'002', name:'ql'})
WriteResult({ "nInserted" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f4fcf3c8cf279cf89ad19"), "sn" : "001", "name" : "lisi" }
{ "_id" : 2, "sn" : "002", "name" : "ql" }
>
- 增加多篇文档, 注意中括号[]
> db.stu.insert([{name:'a', gender:0},{name:'b', gneder:0},{name:'c', gender:1},{name:'d',gender:1}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 4,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
> db.stu.find()
{ "_id" : ObjectId("572f56ca3c8cf279cf89ad23"), "name" : "a", "gender" : 0 }
{ "_id" : ObjectId("572f56ca3c8cf279cf89ad24"), "name" : "b", "gender" : 0 }
{ "_id" : ObjectId("572f56ca3c8cf279cf89ad25"), "name" : "c", "gender" : 1 }
{ "_id" : ObjectId("572f56ca3c8cf279cf89ad26"), "name" : "d", "gender" : 1 }
>
删除数据库
db.dropDatabase()
删除集合
db.collection_name.drop()
删除文档中某条记录, db.collection.remove(查询表达式, 选项)
查询表达式
- 仍然是json对象
- 查询表达式匹配的行将被删掉
- 如果不写, 这个collection将被删掉
选项
- 指justOne:true, 是否只删除一行, 默认为false
> db.stu.find()
{ "_id" : ObjectId("572f4fcf3c8cf279cf89ad19"), "sn" : "001", "name" : "lisi" }
{ "_id" : 2, "sn" : "002", "name" : "ql" }
{ "_id" : 3, "sn" : "003", "name" : "zhangfei" }
>
> db.stu.remove({sn:'002'})
WriteResult({ "nRemoved" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f4fcf3c8cf279cf89ad19"), "sn" : "001", "name" : "lisi" }
{ "_id" : 3, "sn" : "003", "name" : "zhangfei" }
>
> db.stu.find()
{ "_id" : ObjectId("572f57c83c8cf279cf89ad27"), "name" : "a", "gender" : 0 }
{ "_id" : ObjectId("572f57c83c8cf279cf89ad28"), "name" : "b", "gender" : 0 }
{ "_id" : ObjectId("572f57c83c8cf279cf89ad29"), "name" : "c", "gender" : 1 }
{ "_id" : ObjectId("572f57c83c8cf279cf89ad2a"), "name" : "d", "gender" : 1 }
>
>
> db.stu.remove({gender:0}) #把匹配到的全部删掉
WriteResult({ "nRemoved" : 2 })
>
> db.stu.find()
{ "_id" : ObjectId("572f57c83c8cf279cf89ad29"), "name" : "c", "gender" : 1 }
{ "_id" : ObjectId("572f57c83c8cf279cf89ad2a"), "name" : "d", "gender" : 1 }
>
> db.stu.remove({gender:1}, true) #加参数true, 表示只删除一项
WriteResult({ "nRemoved" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f57c83c8cf279cf89ad2a"), "name" : "d", "gender" : 1 }
>
改update
操作
该谁, 改成什么样
语法: db.collection.update(查询表达式, 新值, 选项)
> db.stu.find()
{ "_id" : ObjectId("572f57c83c8cf279cf89ad2a"), "name" : "d", "gender" : 1 }
>
>
>
>
>
> db.stu.update({name:'d'}, {name:'zhangsan'})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f57c83c8cf279cf89ad2a"), "name" : "zhangsan" }
>
改完后gender已经消失了, 因为是新文档直接替换旧文档, 而不是修改
加上$set
参数
> db.stu.find()
{ "_id" : ObjectId("572f59e23c8cf279cf89ad2b"), "name" : "a", "gender" : 0 }
{ "_id" : ObjectId("572f59e23c8cf279cf89ad2c"), "name" : "b", "gender" : 0 }
{ "_id" : ObjectId("572f59e23c8cf279cf89ad2d"), "name" : "c", "gender" : 1 }
{ "_id" : ObjectId("572f59e23c8cf279cf89ad2e"), "name" : "d", "gender" : 1 }
>
>
> db.stu.update({name:'a'}, {$set:{name: 'zhangsan'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f59e23c8cf279cf89ad2b"), "name" : "zhangsan", "gender" : 0 }
{ "_id" : ObjectId("572f59e23c8cf279cf89ad2c"), "name" : "b", "gender" : 0 }
{ "_id" : ObjectId("572f59e23c8cf279cf89ad2d"), "name" : "c", "gender" : 1 }
{ "_id" : ObjectId("572f59e23c8cf279cf89ad2e"), "name" : "d", "gender" : 1 }
>
如果要修改多条文档, 则添加{multi: true}
> db.stu.update({gender:1}, {$set:{name:'zzzzzzz'}}, {multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
>
> db.stu.find().pretty()
{
"_id" : ObjectId("572f59e23c8cf279cf89ad2b"),
"name" : "zhangsan",
"gender" : 0
}
{ "_id" : ObjectId("572f59e23c8cf279cf89ad2c"), "name" : "b", "gender" : 0 }
{
"_id" : ObjectId("572f59e23c8cf279cf89ad2d"),
"name" : "zzzzzzz",
"gender" : 1
}
{
"_id" : ObjectId("572f59e23c8cf279cf89ad2e"),
"name" : "zzzzzzz",
"gender" : 1
}
>
- rename : 重命名某个列
- inc : 增长, 相当于+=. inc只能用于操作数值类型的数据
> db.stu.find()
{ "_id" : ObjectId("572f4e8b3c8cf279cf89ad18"), "name" : "zzzz", "age" : 29 }
>
>
> db.stu.update({'name':'zzzz'}, {$inc:{'age':11}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f4e8b3c8cf279cf89ad18"), "name" : "zzzz", "age" : 40 }
>
- set : 设置新的字段的值
- unset : 删除指定的列
> db.stu.find()
{ "_id" : ObjectId("572f5ece3c8cf279cf89ad3e"), "name" : "a", "age" : 20, "sex" : 1, "hobby" : "basketball" }
>
>
> db.stu.update({name:'a'}, {$set:{name:'lisi'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f5ece3c8cf279cf89ad3e"), "name" : "lisi", "age" : 20, "sex" : 1, "hobby" : "basketball" }
>
> db.stu.update({name:'lisi'}, {$unset:{age:20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f5ece3c8cf279cf89ad3e"), "name" : "lisi", "sex" : 1, "hobby" : "basketball" }
>
> db.stu.update({name:'lisi'}, {$rename:{sex:"gender"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f5ece3c8cf279cf89ad3e"), "name" : "lisi", "hobby" : "basketball", "gender" : 1 }
>
>
增加upsert参数, 没有这行就增加
> db.stu.update({name:'ggg'},{$set:{name:'hhh'}},{upsert:true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("572f60f331e6f9c53c1d493a")
})
>
> db.stu.find()
{ "_id" : ObjectId("572f5ece3c8cf279cf89ad3e"), "name" : "lisi", "hobby" : "basketball", "gender" : 1 }
{ "_id" : ObjectId("572f60f331e6f9c53c1d493a"), "name" : "hhh" }
>
查
- db.collection.find()
查询所有文档所有内容 - db.collection.find().pretty()
查询所有文档所有属性并格式化输出 - db.collection.find({},{demo:1})
查询所有文档的demo属性(_id属性总是默认查出来) - db.collection.find({},{demo:1, _id:0})
查询所有文档的demo属性(去除_id属性)