增删改查
1. 插入文档
> 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
}
> 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. 更新文档
> 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()
>