首页 技术 正文
技术 2022年11月21日
0 收藏 537 点赞 5,056 浏览 5130 个字

  • 增加单篇文档
> 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属性)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919