MongoDB 增删改查

 

 

做增删改查操作前都要先切换到需要操作的数据库

db.表名(Collection).操作(参数)   这个操作是个方法类似于orm操作

 

新建数据库db1

> use db1
switched to db db1

 

1.插入:

MongoDB 之 插入数据(insert()     insertOne()     insertMany()) 之 Collection

insert() :插入一条或者多条数据,需要带有允许插入多条的参数,这个方法目前官方已经不推荐喽

db.user.insert( { id:1,name:"jack",age:73 } )
> db.user.insert( { id:1,name:"jack",age:73 } )
WriteResult({ "nInserted" : 1 })

 

insertOne(): 插入一条数据,官方推荐,效率更高

 

> db.user.insertOne( { id:2,name:"mike",age:84 } )
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5ca78ea8219efd687462f95c")
}

我们可以看出这两张方法返回值不同

 

> db.user.find()
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca78ea8219efd687462f95c"), "id" : 2, "name" : "mike", "age" : 84 }

 

insertMany():插入多条数据,无需参数控制,官方推荐

插入三条数据

> db.user.insertMany([{ id:3,name:"peter",age:14 },{ id:4,name:"tom",age:13 },{ id:5,name:"ben",age:24 }])
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5ca79127219efd687462f95d"),
        ObjectId("5ca79127219efd687462f95e"),
        ObjectId("5ca79127219efd687462f95f")
    ]
}

验证

> db.user.find()
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca78ea8219efd687462f95c"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca79127219efd687462f95d"), "id" : 3, "name" : "peter", "age" : 14 }
{ "_id" : ObjectId("5ca79127219efd687462f95e"), "id" : 4, "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("5ca79127219efd687462f95f"), "id" : 5, "name" : "ben", "age" : 24 }

 

2.查询

MongoDB  之  查询数据(find findOne) 之 这里没有findMany

find() 无条件查找:将该表(Collection)中所有的数据一次性返回

> db.user.find()
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca78ea8219efd687462f95c"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca79127219efd687462f95d"), "id" : 3, "name" : "peter", "age" : 14 }
{ "_id" : ObjectId("5ca79127219efd687462f95e"), "id" : 4, "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("5ca79127219efd687462f95f"), "id" : 5, "name" : "ben", "age" : 24 }

 

db.user.find( { name:"jack" } ) 条件查询 

返回name="jack"数据,这里会返回多条数据

> db.user.find({name:"jack"})
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }

 

findOne():无条件查找一条数据,默认当前Collection中的第一条数据,只返回一条数据

> db.user.findOne()
{
    "_id" : ObjectId("5ca65381bb9b65f40b7e5502"),
    "id" : 1,
    "name" : "jack",
    "age" : 73
}

 

findOne({age:84}) : 条件查找一条age等于84的数据,如有多条数据则返回更靠前的数据

> db.user.findOne({age:84})
{
    "_id" : ObjectId("5ca78ea8219efd687462f95c"),
    "id" : 2,
    "name" : "mike",
    "age" : 84
}

做个测试findOne多条数据,插入多一条age等于84数据,然后再用findOne条件查询

最后返回靠前的数据,并且只返回一条数据

> db.user.insertOne({id:6,name:"Mary",age:84})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5ca798eb219efd687462f960")
}

> db.user.find()
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca78ea8219efd687462f95c"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca79127219efd687462f95d"), "id" : 3, "name" : "peter", "age" : 14 }
{ "_id" : ObjectId("5ca79127219efd687462f95e"), "id" : 4, "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("5ca79127219efd687462f95f"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca798eb219efd687462f960"), "id" : 6, "name" : "Mary", "age" : 84 }

> db.user.findOne({age:84})
{
    "_id" : ObjectId("5ca78ea8219efd687462f95c"),
    "id" : 2,
    "name" : "mike",
    "age" : 84
}

 

3.删除

MongoDB  之  删除数据(remove)

remove({}):无条件删除数据,这里要注意了,这是删除所有数据,清空整张表Collection

 

remove({name:"peter"}) : 条件删除name等于"peter"的所有Document

> db.user.find()
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca78ea8219efd687462f95c"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca79127219efd687462f95d"), "id" : 3, "name" : "peter", "age" : 14 }
{ "_id" : ObjectId("5ca79127219efd687462f95e"), "id" : 4, "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("5ca79127219efd687462f95f"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca798eb219efd687462f960"), "id" : 6, "name" : "Mary", "age" : 84 }

> db.user.remove({"name":"peter"})
WriteResult({ "nRemoved" : 1 })
> db.user.find()
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca78ea8219efd687462f95c"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca79127219efd687462f95e"), "id" : 4, "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("5ca79127219efd687462f95f"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca798eb219efd687462f960"), "id" : 6, "name" : "Mary", "age" : 84 }

 

deleteOne(): 传入要删除的那个字段查询条件,删除一条

> db.user.find()
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca78ea8219efd687462f95c"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca79127219efd687462f95e"), "id" : 4, "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("5ca79127219efd687462f95f"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca798eb219efd687462f960"), "id" : 6, "name" : "Mary", "age" : 84 }

> db.user.deleteOne({name:"ben"}) { "acknowledged" : true, "deletedCount" : 1 } > db.user.find() { "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 } { "_id" : ObjectId("5ca78ea8219efd687462f95c"), "id" : 2, "name" : "mike", "age" : 84 } { "_id" : ObjectId("5ca79127219efd687462f95e"), "id" : 4, "name" : "tom", "age" : 13 } { "_id" : ObjectId("5ca798eb219efd687462f960"), "id" : 6, "name" : "Mary", "age" : 84 }

 

deleteMany(): 删除多条数据

有两条数据age=84的,利用deleteMany方法删除,把age=84的数据都删除,下面删除了两条数据

> db.user.find()
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca78ea8219efd687462f95c"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca79127219efd687462f95e"), "id" : 4, "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("5ca798eb219efd687462f960"), "id" : 6, "name" : "Mary", "age" : 84 }


> db.user.deleteMany({ age:84 })
{ "acknowledged" : true, "deletedCount" : 2 }
> db.user.find()
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca79127219efd687462f95e"), "id" : 4, "name" : "tom", "age" : 13 }

drop() 和 remove一样 可以清空整张表

> db.user.find()
{ "_id" : ObjectId("5ca65381bb9b65f40b7e5502"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca79127219efd687462f95e"), "id" : 4, "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("5ca7a0ff219efd687462f961"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca7a0ff219efd687462f962"), "id" : 3, "name" : "peter", "age" : 14 }
{ "_id" : ObjectId("5ca7a0ff219efd687462f964"), "id" : 6, "name" : "Mary", "age" : 84 }


> db.user.drop()
true
> db.user.find()

 

4.更新

MongoDB 之 修改数据(update updateOne updateMany) 之 跟insert一样,官方不推荐update的写法

update():

update({"name":"peter"},{$set:{"age":21}}):根据条件修改该条数据的内容

把name等于"peter"中数据的age改为21,

这里要注意的是({"条件"},{"关键字":{"修改内容"}}),其中如果条件为空,那么将会修改Collection中所有的数据

 

 

> db.user.find()
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 14 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84 }
> 

> db.user.update( {"name":"peter" }, {$set: {age:21} })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find() { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 } { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84 } { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 } { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "tom", "age" : 13 } { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 } { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84 }

 

 

updateOne():

updateOne({"age":13},{$set:{"name":"xiaogang"}}):根据条件修改一条数据的内容,如出现多条,只修改最高前的数据

把age等于13的所有数据中第一条数据的name改为xiaogang

 

> db.user.find()
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84 }
> 

> 
> db.user.updateOne( {age:13 }, {$set: {"name":"xiaogang"} })
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> 
> db.user.find()
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84 }

 

查询到指定的字段的数据,更新加一个字段

> db.user.find()
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84 }

> db.user.updateOne( {age:13 }, {$set: {hobby:["羽毛球","篮球","足球"]} })
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

> db.user.find()
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84 

 

updateMany():

updateMany({"age":84},{$set:{"gender":"男"}}):根据条件修改所有数据的内容,多条修改

 

把age等于84的所有数据中新增一个字段gender="男"

> db.user.find()
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84 }


> db.user.updateMany( {age:84 }, {$set: {gender:"男"} })
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }

> db.user.find()
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }

 

$set:{"gender":"男"} 解释: $set 是update时的关键字,表示我要设置gender属性的值为"男"

如果该条Documents没有gender属性,他就会自动创建一个gender属性并且赋值为"男"

 

$set是修改器之一 :设置将某key设置为某值

上一篇:arcgis80坐标转84坐标方法


下一篇:多校冲刺 NOIP 20211106 模拟 (24)