mongodb 基本操作

mongodb基本操作

1.基本操作

1.连接mongodb
[mongo@redis01 ~]$ mongo

# 创建用户
添加用户
db.createUser( {
    user: "sa",
    pwd: "sa",
    roles: [ { role: "root", db: "admin" } ]
  });

db.auth('sa','sa');

2.查看库
> show databases 或 > show dbs
admin   0.000GB			#系统预留库
config  0.000GB			#Mongodb配置信息库
local   0.000GB			#本地库,存储日志

3.查看当前库
> db
test					#登录式默认的库

4.切换库
> use admin
switched to db admin

5.查看集合
> show tables 或  show collections
system.version

6.退出mongodb
> exit
> quit()

2.插入数据

1)单条数据插入
> db.jihe.insert({id:'1'})

> db.jihe.insert({id:"2",name:"qiudao",age:"18"})
WriteResult({ "nInserted" : 1 })

> db.jihe.insertOne({id:"3",name:"egon",age:"99",sex:"weizhi"})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("5fd25be228aabf6e1ac24db7")
}
2)多条数据插入
> db.jihe.insertMany([{id:"4",name:"lhd",age:"20",sex:"nan",tall:"182"},{id:"5",name:"zengdao",age:"25",sex:"nan",tall:"180"}])
{
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("5fd25cfe28aabf6e1ac24db8"),
		ObjectId("5fd25cfe28aabf6e1ac24db9")
	]
}
3)多条数据插入,数据多条件
> db.jihe.insertMany([{id:"1",name:"qiudao",age:"18",figure:{tall:"120",weight:"120"}},{id:"2",name:"egon",age:"88",figure:{tall:"160",weight:"200"}},{id:"3",name:"lhd",age:"20",figure:{tall:"182",weight:"220"}}])
{
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("5fd25e6728aabf6e1ac24dba"),
		ObjectId("5fd25e6728aabf6e1ac24dbb"),
		ObjectId("5fd25e6728aabf6e1ac24dbc")
	]
}

3.查询数据

1)查询集合所有数据
> db.jihe.find()
2)查询一条数据
#查看第一条数据,始终是第一条
> db.jihe.findOne()
{ "_id" : ObjectId("5fd25b5228aabf6e1ac24db5"), "id" : "1" }
3)指定条件查询
#条件一定要加引号
> db.jihe.findOne({name:"qiudao"})
{
	"_id" : ObjectId("5fd25b8928aabf6e1ac24db6"),
	"id" : "2",
	"name" : "qiudao",
	"age" : "18"
}
4)查询指定多条件
#表示多条件同时匹配
> db.jihe.find({name:"qiudao",id:"1"})
{ "_id" : ObjectId("5fd25e6728aabf6e1ac24dba"), "id" : "1", "name" : "qiudao", "age" : "18", "figure" : { "tall" : "120", "weight" : "120" } }
{ "_id" : ObjectId("5fd25efe28aabf6e1ac24dbd"), "id" : "1", "name" : "qiudao", "age" : "18", "figure" : { "tall" : "120", "weight" : "120" } }

#表示多条件或者匹配
> db.jihe.find({$or:[{name:"qiudao"},{name:"lhd"}]})
{ "_id" : ObjectId("5fd25b8928aabf6e1ac24db6"), "id" : "2", "name" : "qiudao", "age" : "18" }
{ "_id" : ObjectId("5fd25cfe28aabf6e1ac24db8"), "id" : "4", "name" : "lhd", "age" : "20", "sex" : "nan", "tall" : "182" }
{ "_id" : ObjectId("5fd25e6728aabf6e1ac24dba"), "id" : "1", "name" : "qiudao", "age" : "18", "figure" : { "tall" : "120", "weight" : "120" } }
{ "_id" : ObjectId("5fd25e6728aabf6e1ac24dbc"), "id" : "3", "name" : "lhd", "age" : "20", "figure" : { "tall" : "182", "weight" : "220" } }
{ "_id" : ObjectId("5fd25efe28aabf6e1ac24dbd"), "id" : "1", "name" : "qiudao", "age" : "18", "figure" : { "tall" : "120", "weight" : "120" } }
{ "_id" : ObjectId("5fd25efe28aabf6e1ac24dbf"), "id" : "3", "name" : "lhd", "age" : "20", "figure" : { "tall" : "182", "weight" : "220" } }
5)查询指定范围条件
#查找年龄小于20的
> db.jihe.find({age:{$lt:"20"}})
{ "_id" : ObjectId("5fd25b8928aabf6e1ac24db6"), "id" : "2", "name" : "qiudao", "age" : "18" }
{ "_id" : ObjectId("5fd25e6728aabf6e1ac24dba"), "id" : "1", "name" : "qiudao", "age" : "18", "figure" : { "tall" : "120", "weight" : "120" } }
{ "_id" : ObjectId("5fd25efe28aabf6e1ac24dbd"), "id" : "1", "name" : "qiudao", "age" : "18", "figure" : { "tall" : "120", "weight" : "120" } }

#查找年龄大于30的
> db.jihe.find({age:{$gt:"30"}})
{ "_id" : ObjectId("5fd25be228aabf6e1ac24db7"), "id" : "3", "name" : "egon", "age" : "99", "sex" : "weizhi" }
{ "_id" : ObjectId("5fd25e6728aabf6e1ac24dbb"), "id" : "2", "name" : "egon", "age" : "88", "figure" : { "tall" : "160", "weight" : "200" } }
{ "_id" : ObjectId("5fd25efe28aabf6e1ac24dbe"), "id" : "2", "name" : "egon", "age" : "88", "figure" : { "tall" : "160", "weight" : "200" } }

#查找年龄在20到50之间的
> db.jihe.find({age:{$gt:"20",$lt:"50"}})
{ "_id" : ObjectId("5fd25cfe28aabf6e1ac24db9"), "id" : "5", "name" : "zengdao", "age" : "25", "sex" : "nan", "tall" : "180" }

#查找年龄大于10岁或身高大于120的
> db.jihe.find({$or:[{age:{$gt:"10"}},{"figure.tall":{$gt:"120"}}]})
{ "_id" : ObjectId("5fd25b8928aabf6e1ac24db6"), "id" : "2", "name" : "qiudao", "age" : "18" }
{ "_id" : ObjectId("5fd25be228aabf6e1ac24db7"), "id" : "3", "name" : "egon", "age" : "99", "sex" : "weizhi" }
{ "_id" : ObjectId("5fd25cfe28aabf6e1ac24db8"), "id" : "4", "name" : "lhd", "age" : "20", "sex" : "nan", "tall" : "182" }
{ "_id" : ObjectId("5fd25cfe28aabf6e1ac24db9"), "id" : "5", "name" : "zengdao", "age" : "25", "sex" : "nan", "tall" : "180" }
{ "_id" : ObjectId("5fd25e6728aabf6e1ac24dba"), "id" : "1", "name" : "qiudao", "age" : "18", "figure" : { "tall" : "120", "weight" : "120" } }
{ "_id" : ObjectId("5fd25e6728aabf6e1ac24dbb"), "id" : "2", "name" : "egon", "age" : "88", "figure" : { "tall" : "160", "weight" : "200" } }
{ "_id" : ObjectId("5fd25e6728aabf6e1ac24dbc"), "id" : "3", "name" : "lhd", "age" : "20", "figure" : { "tall" : "182", "weight" : "220" } }
{ "_id" : ObjectId("5fd25efe28aabf6e1ac24dbd"), "id" : "1", "name" : "qiudao", "age" : "18", "figure" : { "tall" : "120", "weight" : "120" } }
{ "_id" : ObjectId("5fd25efe28aabf6e1ac24dbe"), "id" : "2", "name" : "egon", "age" : "88", "figure" : { "tall" : "160", "weight" : "200" } }
{ "_id" : ObjectId("5fd25efe28aabf6e1ac24dbf"), "id" : "3", "name" : "lhd", "age" : "20", "figure" : { "tall" : "182", "weight" : "220" } }

4.修改数据

1)修改一条数据
> db.jihe.updateOne({name:"qiudao"},{$set:{age:"222"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

> db.jihe.find({name:"qiudao"})
{ "_id" : ObjectId("5fd25b8928aabf6e1ac24db6"), "id" : "2", "name" : "qiudao", "age" : "222" }
{ "_id" : ObjectId("5fd25e6728aabf6e1ac24dba"), "id" : "1", "name" : "qiudao", "age" : "18", "figure" : { "tall" : "120", "weight" : "120" } }
{ "_id" : ObjectId("5fd25efe28aabf6e1ac24dbd"), "id" : "1", "name" : "qiudao", "age" : "18", "figure" : { "tall" : "120", "weight" : "120" } }
2)修改多条数据
> db.jihe.updateMany({name:"qiudao"},{$set:{age:"222"}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 2 }

> db.jihe.find({name:"qiudao"})
{ "_id" : ObjectId("5fd25b8928aabf6e1ac24db6"), "id" : "2", "name" : "qiudao", "age" : "222" }
{ "_id" : ObjectId("5fd25e6728aabf6e1ac24dba"), "id" : "1", "name" : "qiudao", "age" : "222", "figure" : { "tall" : "120", "weight" : "120" } }
{ "_id" : ObjectId("5fd25efe28aabf6e1ac24dbd"), "id" : "1", "name" : "qiudao", "age" : "222", "figure" : { "tall" : "120", "weight" : "120" } }

5.删除数据

1)删除单条数据
> db.jihe.deleteOne({id:"1"})
{ "acknowledged" : true, "deletedCount" : 1 }
2)删除多条数据
> db.jihe.deleteMany({id:"1"})
{ "acknowledged" : true, "deletedCount" : 2 }
3)删除集合
> db.jihe.drop()
true
6.建库
#切换库,库不存在也可以切换
> use database
switched to db database

#插入数据建集合
> db.table.insert({id:"1"})
WriteResult({ "nInserted" : 1 })

#查看集合
> show tables
table

#查看库,库已经建好了
> show databases
admin     0.000GB
config    0.000GB
database  0.000GB
local     0.000GB
test      0.000GB
posted @ 2020-12-10 19:52  nick_xm  阅读(91)  评论(0编辑  收藏  举报