MongoDB增删改查

增删改查

1. 插入文档

# db.collection.insert({“键名1”:值1, “键名2”: 值2 …})
> use test
switched to db test
> db.books.insert({"name":"Python入门","price":48})
WriteResult({ "nInserted" : 1 })
> db.books.insert(
... {"name":"C语言管理",
... price:32}
... )
WriteResult({ "nInserted" : 1 })
> db.books.find()
{ "_id" : ObjectId("603f537bbeba72070b372f39"), "name" : "Python入门", "price" : 48 }
{ "_id" : ObjectId("603f53a2beba72070b372f3a"), "name" : "C语言管理", "price" : 32 }

# 利用中括号,实现一次多条文档插入
> db.books.insert( [ {"name":"小学教材","price":20}, {"name":"初中教材","price":30}, {"name":"高中教材","price":40} ])
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 3,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})

# 可以使用变量方式插入文档
> document = ({"name":"英文教材",price:49})
{ "name" : "英文教材", "price" : 49 }
> db.books.insert(document)
WriteResult({ "nInserted" : 1 })

2. 查询文档

# 查询集合记录
> db.books.find()
{ "_id" : ObjectId("603f537bbeba72070b372f39"), "name" : "Python入门", "price" : 48 }
{ "_id" : ObjectId("603f53a2beba72070b372f3a"), "name" : "C语言管理", "price" : 32 }
{ "_id" : ObjectId("603f548fbeba72070b372f3b"), "name" : "小学教材", "price" : 20 }
{ "_id" : ObjectId("603f548fbeba72070b372f3c"), "name" : "初中教材", "price" : 30 }
{ "_id" : ObjectId("603f548fbeba72070b372f3d"), "name" : "高中教材", "price" : 40 }
{ "_id" : ObjectId("603f54dfbeba72070b372f3e"), "name" : "英文教材", "price" : 49 }

# 查询一条记录
> db.books.findOne()
{
	"_id" : ObjectId("603f537bbeba72070b372f39"),
	"name" : "Python入门",
	"price" : 48
}

# 格式查询所有记录
> db.books.find().pretty()
{
	"_id" : ObjectId("603f537bbeba72070b372f39"),
	"name" : "Python入门",
	"price" : 48
}
{
	"_id" : ObjectId("603f53a2beba72070b372f3a"),
	"name" : "C语言管理",
	"price" : 32
}
{
	"_id" : ObjectId("603f548fbeba72070b372f3b"),
	"name" : "小学教材",
	"price" : 20
}
{
	"_id" : ObjectId("603f548fbeba72070b372f3c"),
	"name" : "初中教材",
	"price" : 30
}
{
	"_id" : ObjectId("603f548fbeba72070b372f3d"),
	"name" : "高中教材",
	"price" : 40
}
{
	"_id" : ObjectId("603f54dfbeba72070b372f3e"),
	"name" : "英文教材",
	"price" : 49
}

# 等价条件查询
> db.books.find({price:30}).pretty()
{
	"_id" : ObjectId("603f548fbeba72070b372f3c"),
	"name" : "初中教材",
	"price" : 30
}

# 带$in运算符的查询
> db.books.find({price:30}).pretty()
{
	"_id" : ObjectId("603f548fbeba72070b372f3c"),
	"name" : "初中教材",
	"price" : 30
}
> db.books.find({price:{$in:[20,30,40]}}).pretty()
{
	"_id" : ObjectId("603f548fbeba72070b372f3b"),
	"name" : "小学教材",
	"price" : 20
}
{
	"_id" : ObjectId("603f548fbeba72070b372f3c"),
	"name" : "初中教材",
	"price" : 30
}
{
	"_id" : ObjectId("603f548fbeba72070b372f3d"),
	"name" : "高中教材",
	"price" : 40
}

# 正则实例:
## 查询值的固定后一部分
> db.books.find({name:{$regex:/教材$/}}).pretty()
{
	"_id" : ObjectId("603f548fbeba72070b372f3b"),
	"name" : "小学教材",
	"price" : 20
}
{
	"_id" : ObjectId("603f548fbeba72070b372f3c"),
	"name" : "初中教材",
	"price" : 30
}
{
	"_id" : ObjectId("603f548fbeba72070b372f3d"),
	"name" : "高中教材",
	"price" : 40
}
{
	"_id" : ObjectId("603f54dfbeba72070b372f3e"),
	"name" : "英文教材",
	"price" : 49
}

## 查询值的固定前一部分
> db.books.find({name:{$regex:/^C语言/}}).pretty()
{
	"_id" : ObjectId("603f53a2beba72070b372f3a"),
	"name" : "C语言管理",
	"price" : 32
}

## 查询值的任意部分
> db.books.find({name:{$regex:/Python/}}).pretty()
{
	"_id" : ObjectId("603f537bbeba72070b372f39"),
	"name" : "Python入门",
	"price" : 48
}

# 区间条件查找
> db.books.find({price:{$gt:15,$lt:35}}).pretty()
{
	"_id" : ObjectId("603f53a2beba72070b372f3a"),
	"name" : "C语言管理",
	"price" : 32
}
{
	"_id" : ObjectId("603f548fbeba72070b372f3b"),
	"name" : "小学教材",
	"price" : 20
}
{
	"_id" : ObjectId("603f548fbeba72070b372f3c"),
	"name" : "初中教材",
	"price" : 30
}

3. 更新文档

# 在集合order里,先插入一条订单信息,然后用update命令修改订单名称。
> db.order.insert({
... title:'商品购物单1',
... amount:35,
... detail:[
...     {name:'苹果',price:22},{name:'面粉',price:18}
...        ]
... })
WriteResult({ "nInserted" : 1 })
> db.order.update({title:'商品购物单1'},{$set:{title:'商品购物单2'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

# 数值运算
> db.order.update(
... {
... title:"商品购物单2"
... },
... {$inc:{amount:5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

# 修改一条文档里的数组和嵌套文档
> db.order.insert({
... _id:12,
... title:"商品购物单5",
... amount:90,
... unit:"元",
... detail:[
...    {name:"葡萄",price:60},{name:"面粉",price:30}
... ],
... overview:{shop:"京东电子商务平台",shopno:5,address:"地球村"}
... })
WriteResult({ "nInserted" : 1 })
> db.order.update(
... {_id:12},
... {
... $set:{
...       "detail.1":{name:"大米",price:40},
...       "overview.address":"北京市丰台区南里道15号"
...       }
... })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.order.find({_id:12}).pretty()
{
	"_id" : 12,
	"title" : "商品购物单5",
	"amount" : 90,
	"unit" : "元",
	"detail" : [
		{
			"name" : "葡萄",
			"price" : 60
		},
		{
			"name" : "大米",
			"price" : 40
		}
	],
	"overview" : {
		"shop" : "京东电子商务平台",
		"shopno" : 5,
		"address" : "北京市丰台区南里道15号"
	}
}

# 多文档修改
> db.order.update(
... {"detail.name":"面粉","detail.price":{$lte:30}},
... { $set:{
...         "detail.1":{name:"面粉",price:40}
...         }
... },
... {multi:true}
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

4. 删除文档

> db.tests.insertMany([
...   {item:"铅笔",price:2},
...   {item:"钢笔",price:60}
... ])
{
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("603f5942beba72070b372f40"),
		ObjectId("603f5942beba72070b372f41")
	]
}
> db.tests.remove( { price:{$lt:3} } )
WriteResult({ "nRemoved" : 1 })
> db.tests.find()
{ "_id" : ObjectId("603f5942beba72070b372f41"), "item" : "钢笔", "price" : 60 }
> db.tests.remove( { } )
WriteResult({ "nRemoved" : 1 })
> db.tests.find()
> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值