MongoDB基本操作【增、删、改、查】
1、库(database)操作
以下实例我们创建了数据库 db1:
> use db1
switched to db db1
> db
db1
如果你想查看所有数据库,可以使用 show dbs 命令:
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
可以看到,我们刚创建的数据库 db1 并不在数据库的列表中,要显示它,我们需要向 db1 数据库插入一些数据:
> db.db1.insert({_id:1,test:"hello MongoDB"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
db1 0.000GB
local 0.000GB
以下实例我们删除了数据库 db1:
> db.dropDatabase()
{ "dropped" : "db1", "ok" : 1 }
可以看到数据库 db1 已经不存在了:
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
2、集合(collection)操作
创建数据库 test 为后续测试做准备:
> use test
switched to db test
> db
test
> db.createCollection("student")
{ "ok" : 1 }
以下实例我们创建了集合 student:
> db.createCollection("student")
{ "ok" : 1 }
如果你想查看该库的所有集合,可以使用 db.getCollectionNames() 命令:
> db.getCollectionNames()
[ "student" ]
获取指定名称的聚集集合(table)【个人感觉没有实际意义(集合可以不存在)】:
> db.getCollection("student")
test.student
> db.getCollection("t1")
test.t1
//可以看到上方“t1”这个集合实际不存在,但仍然可以返回聚集集合
显示当前db所有聚集索引的状态
> db.printCollectionStats()
其它
查询之前的错误信息
db.getPrevError();
清除错误记录
db.resetError();
查看帮助
db.collectionName.help()
查询当前集合的数据条数
db.collectionName.count()
查看数据空间大小
db.collectionName.dataSize()
得到当前聚集集合所在的db
db.collectionName.getDB()
得到当前聚集的状态
db.collectionName.stats()
得到聚集集合总大小
db.collectionName.totalSize()
聚集集合储存空间大小
db.collectionName.storageSize()
Shard版本信息
db.collectionName.getShardVersion()
聚集集合重命名
db.collectionName.renameCollection("newCollectionName")
删除当前聚集集合
db.collectionName.drop()
3、文档(documen)操作
1)插入(增添)操作
以下实例我们在 db1.student 集合中插入一个文档:
方法一:
> db.student.insert({id:1,name:"张三",gender:"男",age:18})
WriteResult({ "nInserted" : 1 })
方法二:
> db.student.save({id:2,name:"李四",gender:"男",age:19})
WriteResult({ "nInserted" : 1 })
方法三:
> db.student.insertOne({id:3,name:"王五",gender:"男",age:18})
{
"acknowledged" : true,
"insertedId" : ObjectId("62986a0622e54ae6c619dde6")
}
以下实例我们在 db1.student 集合中插入多个文档:
> db.student.insertMany(
... [
... {id:4,name:"赵六",gender:"女",age:21},
... {id:5,name:"钱七",gender:"女",age:20}
... ]
... )
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62986bbe22e54ae6c619dde8"),
ObjectId("62986bbe22e54ae6c619dde9")
]
}
以下实例我们在 db1.student 集合中插入多个多维文档:
> db.student.insertMany(
... [
... {id:8,name:"孙八",gender:"男",age:19,address:{province:"guangdong",city:"guangzhou"}},
... {id:9,name:"侯九",gender:"女",age:22,address:{province:"guangxi",city:"guilin"}},
... {id:10,name:"熊十",gender:"女",age:18,address:{province:"fujian",city:"xiamen"}}
... ]
... )
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62986d4e22e54ae6c619ddea"),
ObjectId("62986d4e22e54ae6c619ddeb"),
ObjectId("62986d4e22e54ae6c619ddec")
]
}
以下实例我们在 db1.student 集合中插入多个多维数组文档:
> db.student.insertMany(
... [
... {id:11,name:"吴京",gender:"男",age:43,address:{province:"beijing",city:"beijing"},hobby:["mil","fight","sing"]},
... {id:12,name:"杨幂",gender:"女",age:31,address:{province:"hongkong",city:"hongkong"},hobby:["dance","shopping","sing"]},
... {id:13,name:"黄渤",gender:"男",age:45,address:{province:"shandong",city:"qingdao"},hobby:["dance","game","sing"]}
... ]
... )
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62986f8322e54ae6c619dded"),
ObjectId("62986f8322e54ae6c619ddee"),
ObjectId("62986f8322e54ae6c619ddef")
]
}
2)查询操作
查询所有文档:
> db.student.find()
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "name" : "张三", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "id" : 2, "name" : "李四", "gender" : "男", "age" : 19 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "name" : "赵六", "gender" : "女", "age" : 21 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "id" : 5, "name" : "钱七", "gender" : "女", "age" : 20 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "id" : 6, "name" : "孙八", "gender" : "男", "age" : 19, "address" : { "province" : "guangdong", "city" : "guangzhou" } }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "id" : 7, "name" : "侯九", "gender" : "女", "age" : 22, "address" : { "province" : "guangxi", "city" : "guilin" } }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 8, "name" : "熊十", "gender" : "女", "age" : 18, "address" : { "province" : "fujian", "city" : "xiamen" } }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 9, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 10, "name" : "杨幂", "gender" : "女", "age" : 31, "address" : { "province" : "hongkong", "city" : "hongkong" }, "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 11, "name" : "黄渤", "gender" : "男", "age" : 45, "address" : { "province" : "shandong", "city" : "qingdao" }, "hobby" : [ "dance", "game", "sing" ] }
查询所有文档(美化后):
> db.student.find().pretty()
{
"_id" : ObjectId("6298694722e54ae6c619dde4"),
"id" : 1,
"name" : "张三",
"gender" : "男",
"age" : 18
}
{
"_id" : ObjectId("629869de22e54ae6c619dde5"),
"id" : 2,
"name" : "李四",
"gender" : "男",
"age" : 19
}
{
"_id" : ObjectId("62986a0622e54ae6c619dde6"),
"id" : 3,
"name" : "王五",
"gender" : "男",
"age" : 18
}
{
"_id" : ObjectId("62986bbe22e54ae6c619dde8"),
"id" : 4,
"name" : "赵六",
"gender" : "女",
"age" : 21
}
{
"_id" : ObjectId("62986bbe22e54ae6c619dde9"),
"id" : 5,
"name" : "钱七",
"gender" : "女",
"age" : 20
}
{
"_id" : ObjectId("62986d4e22e54ae6c619ddea"),
"id" : 6,
"name" : "孙八",
"gender" : "男",
"age" : 19,
"address" : {
"province" : "guangdong",
"city" : "guangzhou"
}
}
{
"_id" : ObjectId("62986d4e22e54ae6c619ddeb"),
"id" : 7,
"name" : "侯九",
"gender" : "女",
"age" : 22,
"address" : {
"province" : "guangxi",
"city" : "guilin"
}
}
{
"_id" : ObjectId("62986d4e22e54ae6c619ddec"),
"id" : 8,
"name" : "熊十",
"gender" : "女",
"age" : 18,
"address" : {
"province" : "fujian",
"city" : "xiamen"
}
}
{
"_id" : ObjectId("62986f8322e54ae6c619dded"),
"id" : 9,
"name" : "吴京",
"gender" : "男",
"age" : 43,
"address" : {
"province" : "beijing",
"city" : "beijing"
},
"hobby" : [
"mil",
"fight",
"sing"
]
}
{
"_id" : ObjectId("62986f8322e54ae6c619ddee"),
"id" : 10,
"name" : "杨幂",
"gender" : "女",
"age" : 31,
"address" : {
"province" : "hongkong",
"city" : "hongkong"
},
"hobby" : [
"dance",
"shopping",
"sing"
]
}
{
"_id" : ObjectId("62986f8322e54ae6c619ddef"),
"id" : 11,
"name" : "黄渤",
"gender" : "男",
"age" : 45,
"address" : {
"province" : "shandong",
"city" : "qingdao"
},
"hobby" : [
"dance",
"game",
"sing"
]
}
查询一个文档(支持指定):
> db.student.findOne()
{
"_id" : ObjectId("6298694722e54ae6c619dde4"),
"id" : 1,
"name" : "张三",
"gender" : "男",
"age" : 18
}
> db.student.findOne({id:5})
{
"_id" : ObjectId("62986bbe22e54ae6c619dde9"),
"id" : 5,
"name" : "钱七",
"gender" : "女",
"age" : 20
}
> db.student.find({id:3})
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
查询全部文档:
> db.student.find()
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "name" : "张三", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "id" : 2, "name" : "李四", "gender" : "男", "age" : 19 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "name" : "赵六", "gender" : "女", "age" : 21 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "id" : 5, "name" : "钱七", "gender" : "女", "age" : 20 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "id" : 8, "name" : "孙八", "gender" : "男", "age" : 19, "address" : { "province" : "guangdong", "city" : "guangzhou" } }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "id" : 9, "name" : "侯九", "gender" : "女", "age" : 22, "address" : { "province" : "guangxi", "city" : "guilin" } }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 10, "name" : "熊十", "gender" : "女", "age" : 18, "address" : { "province" : "fujian", "city" : "xiamen" } }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "name" : "杨幂", "gender" : "女", "age" : 31, "address" : { "province" : "hongkong", "city" : "hongkong" }, "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 13, "name" : "黄渤", "gender" : "男", "age" : 45, "address" : { "province" : "shandong", "city" : "qingdao" }, "hobby" : [ "dance", "game", "sing" ] }
范围条件查询:
$gt 表示大于
$lt 表示小于
$gte 表示大于等于
$lte 表示小于等于
格式:
操作 格式
等于 {<key>:<value>}
小于 {<key>:{$lt:<value>}}
小于或等于 {<key>:{$lte:<value>}}
大于 {<key>:{$gt:<value>}}
大于或等于 {<key>:{$gte:<value>}}
不等于 {<key>:{$ne:<value>}}
示例:
查询年龄大于40岁
> db.student.find({age:{$gt:30}})
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "name" : "杨幂", "gender" : "女", "age" : 31, "address" : { "province" : "hongkong", "city" : "hongkong" }, "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 13, "name" : "黄渤", "gender" : "男", "age" : 45, "address" : { "province" : "shandong", "city" : "qingdao" }, "hobby" : [ "dance", "game", "sing" ] }
多条件查询:
AND db.数据表.find({条件,条件,条件})
OR 条件语句使用了关键字
$or语法格式:
db.数据表.find({$or:[{条件},{条件},{条件}]})
示例:
查询年龄大于等于20岁小于等于30岁
> db.student.find({age:{$gte:20,$lte:30}})
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "name" : "赵六", "gender" : "女", "age" : 21 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "id" : 5, "name" : "钱七", "gender" : "女", "age" : 20 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "id" : 9, "name" : "侯九", "gender" : "女", "age" : 22, "address" : { "province" : "guangxi", "city" : "guilin" } }
支持正则表达式:
查看姓的数据
> db.student.find({name:/^王/})
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
查看名字中含有京的数据
> db.student.find({name:/京/})
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
查询指定列name、age数据(只列出name & age,其它省略):
> db.student.find({},{name:1,age:1})
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "name" : "张三", "age" : 18 }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "name" : "李四", "age" : 19 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "name" : "王五", "age" : 18 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "name" : "赵六", "age" : 21 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "name" : "钱七", "age" : 20 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "name" : "孙八", "age" : 19 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "name" : "侯九", "age" : 22 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "name" : "熊十", "age" : 18 }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "name" : "吴京", "age" : 43 }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "name" : "杨幂", "age" : 31 }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "name" : "黄渤", "age" : 45 }
显示_id,不显示name、age、address列:
> db.student.find({},{_id:1,name:0,age:0,address:0})
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "gender" : "男" }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "id" : 2, "gender" : "男" }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "gender" : "男" }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "gender" : "女" }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "id" : 5, "gender" : "女" }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "id" : 8, "gender" : "男" }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "id" : 9, "gender" : "女" }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 10, "gender" : "女" }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "gender" : "男", "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "gender" : "女", "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 13, "gender" : "男", "hobby" : [ "dance", "game", "sing" ] }
不显示address列:
> db.student.find({},{address:0})
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "name" : "张三", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "id" : 2, "name" : "李四", "gender" : "男", "age" : 19 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "name" : "赵六", "gender" : "女", "age" : 21 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "id" : 5, "name" : "钱七", "gender" : "女", "age" : 20 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "id" : 8, "name" : "孙八", "gender" : "男", "age" : 19 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "id" : 9, "name" : "侯九", "gender" : "女", "age" : 22 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 10, "name" : "熊十", "gender" : "女", "age" : 18 }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "name" : "杨幂", "gender" : "女", "age" : 31, "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 13, "name" : "黄渤", "gender" : "男", "age" : 45, "hobby" : [ "dance", "game", "sing" ] }
不显示_id列(注:_id列自动生成,也可以自己指定):
> db.student.find({},{_id:0,name:1,age:1})
{ "name" : "张三", "age" : 18 }
{ "name" : "李四", "age" : 19 }
{ "name" : "王五", "age" : 18 }
{ "name" : "赵六", "age" : 21 }
{ "name" : "钱七", "age" : 20 }
{ "name" : "孙八", "age" : 19 }
{ "name" : "侯九", "age" : 22 }
{ "name" : "熊十", "age" : 18 }
{ "name" : "吴京", "age" : 43 }
{ "name" : "杨幂", "age" : 31 }
{ "name" : "黄渤", "age" : 45 }
注:一般情况下,要么只指定不显示的,要么只指定要生成的,但是唯独_id除外(特殊情况),可以一起指定
按照年龄排序:
升序
> db.student.find().sort({age:1})
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "name" : "张三", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 10, "name" : "熊十", "gender" : "女", "age" : 18, "address" : { "province" : "fujian", "city" : "xiamen" } }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "id" : 2, "name" : "李四", "gender" : "男", "age" : 19 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "id" : 8, "name" : "孙八", "gender" : "男", "age" : 19, "address" : { "province" : "guangdong", "city" : "guangzhou" } }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "id" : 5, "name" : "钱七", "gender" : "女", "age" : 20 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "name" : "赵六", "gender" : "女", "age" : 21 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "id" : 9, "name" : "侯九", "gender" : "女", "age" : 22, "address" : { "province" : "guangxi", "city" : "guilin" } }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "name" : "杨幂", "gender" : "女", "age" : 31, "address" : { "province" : "hongkong", "city" : "hongkong" }, "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 13, "name" : "黄渤", "gender" : "男", "age" : 45, "address" : { "province" : "shandong", "city" : "qingdao" }, "hobby" : [ "dance", "game", "sing" ] }
降序
> db.student.find().sort({age:-1})
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 13, "name" : "黄渤", "gender" : "男", "age" : 45, "address" : { "province" : "shandong", "city" : "qingdao" }, "hobby" : [ "dance", "game", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "name" : "杨幂", "gender" : "女", "age" : 31, "address" : { "province" : "hongkong", "city" : "hongkong" }, "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "id" : 9, "name" : "侯九", "gender" : "女", "age" : 22, "address" : { "province" : "guangxi", "city" : "guilin" } }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "name" : "赵六", "gender" : "女", "age" : 21 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "id" : 5, "name" : "钱七", "gender" : "女", "age" : 20 }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "id" : 2, "name" : "李四", "gender" : "男", "age" : 19 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "id" : 8, "name" : "孙八", "gender" : "男", "age" : 19, "address" : { "province" : "guangdong", "city" : "guangzhou" } }
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "name" : "张三", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 10, "name" : "熊十", "gender" : "女", "age" : 18, "address" : { "province" : "fujian", "city" : "xiamen" } }
查询前5条数据:
> db.student.find().limit(5)
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "name" : "张三", "gender" : "男" "age" : 18 }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "id" : 2, "name" : "李四", "gender" : "男" "age" : 19 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男" "age" : 18 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "name" : "赵六", "gender" : "女" "age" : 21 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "id" : 5, "name" : "钱七", "gender" : "女" "age" : 20 }
查询7条以后的数据:
> db.student.find().skip(7)
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 10, "name" : "熊十", "gender" : "女", "age" : 18, "address" : { "province" : "fujian", "city" : "xiamen" } }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "name" : "杨幂", "gender" : "女", "age" : 31, "address" : { "province" : "hongkong", "city" : "hongkong" }, "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 13, "name" : "黄渤", "gender" : "男", "age" : 45, "address" : { "province" : "shandong", "city" : "qingdao" }, "hobby" : [ "dance", "game", "sing" ] }
查询在3-12之间的数据:
> db.student.find().skip(2).limit(8)
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "name" : "赵六", "gender" : "女", "age" : 21 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "id" : 5, "name" : "钱七", "gender" : "女", "age" : 20 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "id" : 8, "name" : "孙八", "gender" : "男", "age" : 19, "address" : { "province" : "guangdong", "city" : "guangzhou" } }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "id" : 9, "name" : "侯九", "gender" : "女", "age" : 22, "address" : { "province" : "guangxi", "city" : "guilin" } }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 10, "name" : "熊十", "gender" : "女", "age" : 18, "address" : { "province" : "fujian", "city" : "xiamen" } }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "name" : "杨幂", "gender" : "女", "age" : 31, "address" : { "province" : "hongkong", "city" : "hongkong" }, "hobby" : [ "dance", "shopping", "sing" ] }
查询20岁以下男性:
> db.student.find({age:{$lte:20},gender:"男"})
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "name" : "张三", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "id" : 2, "name" : "李四", "gender" : "男", "age" : 19 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "id" : 8, "name" : "孙八", "gender" : "男", "age" : 19, "address" : { "province" : "guangdong", "city" : "guangzhou" } }
统计男性个数:
> db.student.find({gender:"男"}).count()
6
多维字段查询:
> db.student.find({"address.city":"beijing"})
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
数组条件限制:
> db.student.find({hobby:"fight"})
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
> db.student.find({hobby:{$all:["dance","sing"]}})
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "name" : "杨幂", "gender" : "女", "age" : 31, "address" : { "province" : "hongkong", "city" : "hongkong" }, "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 13, "name" : "黄渤", "gender" : "男", "age" : 45, "address" : { "province" : "shandong", "city" : "qingdao" }, "hobby" : [ "dance", "game", "sing" ] }
其他查询:
$in查询包含的数据
> db.student.find({age:{$in:[18,20,21,23]}},{name:1,age:1,_id:0})
{ "name" : "张三", "age" : 18 }
{ "name" : "王五", "age" : 18 }
{ "name" : "赵六", "age" : 21 }
{ "name" : "钱七", "age" : 20 }
{ "name" : "熊十", "age" : 18 }
$exists判断字段是否存在
查询存在age字段的数据
> db.student.find({age:{$exists:true}})
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "name" : "张三", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "id" : 2, "name" : "李四", "gender" : "男", "age" : 19 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "name" : "赵六", "gender" : "女", "age" : 21 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde9"), "id" : 5, "name" : "钱七", "gender" : "女", "age" : 20 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "id" : 8, "name" : "孙八", "gender" : "男", "age" : 19, "address" : { "province" : "guangdong", "city" : "guangzhou" } }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "id" : 9, "name" : "侯九", "gender" : "女", "age" : 22, "address" : { "province" : "guangxi", "city" : "guilin" } }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 10, "name" : "熊十", "gender" : "女", "age" : 18, "address" : { "province" : "fujian", "city" : "xiamen" } }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "name" : "杨幂", "gender" : "女", "age" : 31, "address" : { "province" : "hongkong", "city" : "hongkong" }, "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 13, "name" : "黄渤", "gender" : "男", "age" : 45, "address" : { "province" : "shandong", "city" : "qingdao" }, "hobby" : [ "dance", "game", "sing" ] }
查询不存在age字段的数据
> db.student.find({age:{$exists:false}})
$mod取模运算
查询满足age列的数值除5余3的数据
> db.student.find({age:{$mod:[5,3]}})
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "name" : "张三", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 10, "name" : "熊十", "gender" : "女", "age" : 18, "address" : { "province" : "fujian", "city" : "xiamen" } }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
$ne不等于操作
查询年龄年龄不是18和20的数据
> db.student.find({age:{$ne:18},age:{$ne:20}})
{ "_id" : ObjectId("6298694722e54ae6c619dde4"), "id" : 1, "name" : "张三", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("629869de22e54ae6c619dde5"), "id" : 2, "name" : "李四", "gender" : "男", "age" : 19 }
{ "_id" : ObjectId("62986a0622e54ae6c619dde6"), "id" : 3, "name" : "王五", "gender" : "男", "age" : 18 }
{ "_id" : ObjectId("62986bbe22e54ae6c619dde8"), "id" : 4, "name" : "赵六", "gender" : "女", "age" : 21 }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddea"), "id" : 8, "name" : "孙八", "gender" : "男", "age" : 19, "address" : { "province" : "guangdong", "city" : "guangzhou" } }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddeb"), "id" : 9, "name" : "侯九", "gender" : "女", "age" : 22, "address" : { "province" : "guangxi", "city" : "guilin" } }
{ "_id" : ObjectId("62986d4e22e54ae6c619ddec"), "id" : 10, "name" : "熊十", "gender" : "女", "age" : 18, "address" : { "province" : "fujian", "city" : "xiamen" } }
{ "_id" : ObjectId("62986f8322e54ae6c619dded"), "id" : 11, "name" : "吴京", "gender" : "男", "age" : 43, "address" : { "province" : "beijing", "city" : "beijing" }, "hobby" : [ "mil", "fight", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddee"), "id" : 12, "name" : "杨幂", "gender" : "女", "age" : 31, "address" : { "province" : "hongkong", "city" : "hongkong" }, "hobby" : [ "dance", "shopping", "sing" ] }
{ "_id" : ObjectId("62986f8322e54ae6c619ddef"), "id" : 13, "name" : "黄渤", "gender" : "男", "age" : 45, "address" : { "province" : "shandong", "city" : "qingdao" }, "hobby" : [ "dance", "game", "sing" ] }
$nin查询不包含的数据
> db.student.find({age:{$nin:[18,20,21,23]}},{name:1,age:1,_id:0})
{ "name" : "李四", "age" : 19 }
{ "name" : "孙八", "age" : 19 }
{ "name" : "侯九", "age" : 22 }
{ "name" : "吴京", "age" : 43 }
{ "name" : "杨幂", "age" : 31 }
{ "name" : "黄渤", "age" : 45 }
$size数组元素个数
不演示(懒)
3)更新操作
update() 方法用于更新已存在的文档
语法格式:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
参数说明:
• query:update的查询条件,类似sql update查询内where后面的;
• update:update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的;
• upsert:可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入;
• multi:可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新;
• writeConcern:可选,抛出异常的级别;
示例:
将title更新为MySQL教程
MongoDB Enterprise > db.books.find()
{ "_id" : ObjectId("599026a4124716992d85a2ed"), "title" : "MongDB教程", "price" : 75, "num" : 50 }
MongoDB Enterprise > db.books.update({"title" : "MongDB教程"},{$set:{"title" : "MySQL教程"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.books.find()
{ "_id" : ObjectId("599026a4124716992d85a2ed"), "title" : "MySQL教程", "price" : 75, "num" : 50 }
price数值加1
MongoDB Enterprise > db.books.update({"title" : "MySQL教程"},{$inc:{"price" : 1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.books.find()
{ "_id" : ObjectId("599026a4124716992d85a2ed"), "title" : "MySQL教程", "price" : 76, "num" : 50 }
db.表.update({条件},{'$set' :{字段:值,字段:值}})
db.表.update({条件},{字段:值,字段:值})
有$set的修改:只修改设置的字段,其他字段不变
没有$set的修改:只修改设置的字段,没有修改的字段就删除了(_id除外)
注:字段有则修改没有则添加新字段
4)删除操作
删除记录:db.表.remove(条件)
删除字段: db.表update({条件},{ ‘$unset’ :{字段:1/字段:0}})