第二章:MongoDB简单的增删改查
来源:互联网 发布:淘宝模特拍照24姿势 编辑:程序博客网 时间:2024/04/30 04:47
一: Insert操作
① 单条插入
var single={
"name":"tom","password":"123456","age":24,"address":{"province":"hubei","city":"shiyan"},"loves":["eat","play","drink"]
}
var single2={
"name":"tom2","password":"123456","age":20,"address":{"province":"shenzhen","city":"dong guan"},"loves":["eat","play","drink"]
}
var single3={
"name":"tom3","password":"123456","age":20,"address":{"province":"hunan","city":"changsha"},"loves":["eat","play","drink"]
}
var single4={
"name":"tom4","password":"123456","age":20,"address":{"province":"guangdong","city":"shiyan"},"loves":["eat","play","drink"]
}
var single5={
"name":"tom","password":"123456","age":44,"address":{"province":"hubei","city":"shiyan"},"loves":["eat","play","drink"]
}
> single.name='tom' 修改name为tom
tom
tom
> single.age=22 修改年龄为22
> db.user.insert(single)
WriteResult({ "nInserted" : 1 })
WriteResult({ "nInserted" : 1 })
> db.user.find()
{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
二:Find操作
日常开发中,查询用的比较多。
①: >, >=, <, <=, !=, =。
②:And,OR,In,NotIn
这些操作在mongodb里面都封装好了,下面就一一介绍:
<1>"$gt", "$gte", "$lt", "$lte", "$ne", "没有特殊关键字",这些跟上面是一一对应的,举几个例子。
/ * find age>22 */
> db.user.find({"age":{$gt:22}})
{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
/ * find age<22 */
> db.user.find({"age":{$lt:22}})
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
/ * find age!=22 */
> db.user.find({"age":{$ne:22}})
{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
/ * find age==22 */
> db.user.find({"age":20})
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "
eat", "play", "drink" ] }
eat", "play", "drink" ] }
<2> "无关键字“, "$or", "$in","$nin" 同样我也是举几个例子
/* find name='tom' && province='hubei'*/ 查询名字为tom 并且 province=湖北的记录
> db.user.find({"name":"tom2","address.province":"hubei"})
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
/* find province='hubei' || province='hunan'*/ 查询 province='hubei' 或者 province='hunan' 的记录
> db.user.find({$or:[{"address.province":"hubei"},{"address.province":"hunan"}]})
{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
/* find province in ["hubei","hunan"]*/
> db.user.find({"address.province":{$in:["hubei","hunan"]}})
{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473382f2122f816c6dec18"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473382f2122f816c6dec18"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
/* find province not in ["hubei","hunan"]*/
> db.user.find({"address.province":{$nin:["hubei","hunan"]}})
{ "_id" : ObjectId("5547369cf2122f816c6dec19"), "name" : "tom3", "password" : "123456", "age" : 20, "address" : { "province" : "guangdong", "city" : "shiyan" }, "loves" :[ "eat", "play", "drink" ] }
<3> 在mongodb中还有一个特殊的匹配,那就是“正则表达式”,功能很强大。
/* find name startwith 't' and endwith 'm' */
> db.user.find({"name":/^t/,"name":/m$/})
{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
<4>$where 处理很复杂。
/* find name='tom2' */
> db.user.find({"name":"tom2"})
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473382f2122f816c6dec18"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473382f2122f816c6dec18"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
> db.user.find({$where:function(){return this.name=='tom2'}})
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473382f2122f816c6dec18"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473382f2122f816c6dec18"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
三:Update操作
<1> 整体更新
/* update tom age=33 */
> var tage=db.user.findOne({"name":'tom'})
> tage.age=33
> db.user.update({"name":"tom"},tage)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 33, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
<2>上传大对象文件
mongofiles put testfile
D:\Program Files\mongodb\Server\3.1\bin>mongofiles put c:/quxian.sql 将c盘里面的quanxian.sql 这个文件上传到数据库
2015-05-05T15:25:39.442+0800 connected to: localhost
> show collections
fs.chunks --fs.chunks
fs.files --fs.files
persion
system.indexes
user
users
>
fs.chunks --fs.chunks
fs.files --fs.files
persion
system.indexes
user
users
>
>db.fs.files.find() --查看files的详细信息
{ "_id" : ObjectId("554870730c94ed15dc000001"), "chunkSize" : 261120, "uploadDate" : ISODate("2015-05-05T07:25:39.995Z"), "length" : 223846, "md5" :
0757d86c8677", "filename" : "c:/quxian.sql" }
0757d86c8677", "filename" : "c:/quxian.sql" }
> db.fs.chunks.find() --查看文件的内容
0 0
- 第二章:MongoDB简单的增删改查
- 第二章:MongoDB简单的增删改查
- mongodb实现简单的增删改查
- mongodb实现简单的增删改查
- MongoDB简单的增删改查
- mongodb实现简单的增删改查
- mongodb-简单增删改查
- MongoDB第二天:MongoDB增删改查
- MongoDB的增删改查
- mongodb的增删改查
- Mongodb的增删改查
- mongoDB搭建和运行简单的增删改查操作
- Spring Data MongoDB简单的增删改查
- mongodb安装与简单的增删改查
- mongodb 增删改查简单封装
- mongodb简单测试增删改查
- Hibernate第二课 单表的简单增删改查
- MongoDB简单的增删改
- 九度OJ-题目1352:和为S的两个数字
- 字典树模板--静态 ----HDU 1251
- json
- NSCache 原理 (著名开源框架SDWebImage 就是使用的NSCache实现的缓存)
- 网站建设的发展前景
- 第二章:MongoDB简单的增删改查
- 学习linux
- 验证手机号码和电话号码的正则表达式
- db file sequential read 详解
- php单例模式的两种方法和检测方法
- Android 中 HttpClient作客户端,Tomcat Servlet作服务器的交互示例
- 深入浅出FPGA-14-ChipScope软件使用 (深入浅出FPGA系列)
- 第三章:MongoDB导出&备份恢复&用户管理
- 手把手教你:多表联合更新