Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add, delete, modify and check MongoDB

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people don't understand the knowledge points of this article "How to add, delete and change MongoDB", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "How to add, delete and change MongoDB" together.

Add an insert to insert a collection into the database

Insert a record into the collection

db.. insert()db.students.insert({name:"Tang Monk",age:60,gender:"M"})

Insert multiple records, pass in array

db.students.insert([{name:"Pig",age:53,gender:"M"},{name:"Sand Monk",age:50,gender:"M"}])

The default timestamp id is generated to ensure that the data is unique. The principle is to call the following statement. You can specify the id yourself.

Default:

ObjectId()

Self-designation:

db.students.insert({_id:"001",name:"White Bone",age:60,gender:"W"})

insertOne向数据库中插入集合

插入一个文档对象,只能传入一个文档,不能传入数组

db.students.insertOne({_id:"002",name:"铁扇公主 ",age:60,gender:"W"})insertMany向数据库中插入集合

插入多个文档对象,只能传入一个数组,即使只存在一个文档,不能传入文档

db.students.insertMany([{_id:"004",name:"牛魔王",age:60,gender:"M"},{_id:"005",name:"红孩儿",age:60,gender:"M"}])

其实是对insert的拆分

二、查询

查询集合中所有符合条件的文档

find进行查询集合所有文档db.students.find();db.students.find({});find进行条件查询

{}:表示集合中所有文档;

{属性:值} 查询属性是指定值的文档

db.students.find({_id:"002"});db.students.find({_id:"002",name:"刘德华"});db.students.find({_id:"002"}).count(); ----查看查询的文档的数量db.students.find({_id:"002"}).length();

find返回值为数组,可以通过下标获取对应值

findOne进行条件查询

findOne返回的为Object,可以用返回值.对象

查询集合中符合条件的第一个文档,最多一条文档

db.students.findOne({name:"AideHua"});三、修改update(查询条件,新对象)

db..update(查询条件,新对象)

如:db.students.update({_id:"002"},{age:18}) 注意:将会用新对象{age:18}替换之前旧对象

修改前

修改后

update(查询条件,{$set:修改文档属性})db.students.update({_id:"004"},{$set:{age:180}})

修改操作符

set ------修改文档指定属性

unset------删除文档指定属性(和值无关)

默认只会修改一个

如果想要进行修改多个,指定可选参数multi

db.students.update({_id:"004"},{$set:{age:880}},{multi:true})updateMany()

同时修改多个符合条件的文档

匹配多少个,就会修改多少个的值

db.students.updateMany({_id:"004"},{$set:{age:180}})updateOne()

修改一个符合条件的文档

只会修改匹配的第一个

db.students.updateOne({_id:"004"},{$set:{age:180}})replaceOne()

替换文档

四、删除remove()

删除符合条件的而所有文档,默认删除所有,删除单个需要指定可选参数justOne为:true

删除多个,默认情况

db.students.remove({name:"Liming"})

删除单个

db.students.remove({name:"Liming"},true)

必须指定参数,否则报错,注意和find()区别

db.students.remove();//报错

删除所有文档,但是集合还是存在的

db.students.remove({});

删除集合,若是数据库的最后一个人集合,那么数据库也会被删除

db.students.drop();deleteOne()

删除一个

db.students.deleteOne({name:"Liming"})deleteMany()

删除多个

db.students.deleteMany({name:"Liming"})以上就是关于"MongoDB怎么实现增删改查"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report