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 implement data increase operation in MongoDB

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

In this issue, the editor will bring you about how to add data in MongoDB. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

As long as it is a database, it is absolutely inseparable from the most core function: CRUD, so the operation of data is also supported in MongoDB, but it should be reminded that everything else is troublesome except for the addition.

1. Data increase

Add data can be achieved by using "db. Collection. Insert ()".

Example: add a simple data

> use hr

Switched to db hr

> db.info.insert ({"url": "www.stone.com"})

WriteResult ({"nInserted": 1})

> db.info.find ()

{"_ id": ObjectId ("5990f8ea3268c8e84253ba3b"), "url": "www.stone.com"}

Example: save an array

> db.info.insert ([{"url": "www.stone.com"}, {"url": "www.stonedb.com"}])

BulkWriteResult ({

"writeErrors": []

"writeConcernErrors": []

"nInserted": 2

"nUpserted": 0

"nMatched": 0

"nModified": 0

"nRemoved": 0

"upserted": []

> db.info.find ()

{"_ id": ObjectId ("5990f8ea3268c8e84253ba3b"), "url": "www.stone.com"}

{"_ id": ObjectId ("5990f97d3268c8e84253ba3c"), "url": "www.stone.com"}

{"_ id": ObjectId ("5990f97d3268c8e84253ba3d"), "url": "www.stonedb.com"}

If you want to save more than one data, use an array.

Example: save 1000 pieces of data

> for (var Xerox db.info.find ()

{"_ id": ObjectId ("5990f8ea3268c8e84253ba3b"), "url": "www.stone.com"}

{"_ id": ObjectId ("5990f97d3268c8e84253ba3c"), "url": "www.stone.com"}

{"_ id": ObjectId ("5990f97d3268c8e84253ba3d"), "url": "www.stonedb.com"}

{"_ id": ObjectId ("5990faea3268c8e84253ba3e"), "url": "stone_0"}

{"_ id": ObjectId ("5990faea3268c8e84253ba3f"), "url": "stone_1"}

{"_ id": ObjectId ("5990faea3268c8e84253ba40"), "url": "stone_2"}

{"_ id": ObjectId ("5990faea3268c8e84253ba41"), "url": "stone_3"}

{"_ id": ObjectId ("5990faea3268c8e84253ba42"), "url": "stone_4"}

{"_ id": ObjectId ("5990faea3268c8e84253ba43"), "url": "stone_5"}

{"_ id": ObjectId ("5990faea3268c8e84253ba44"), "url": "stone_6"}

{"_ id": ObjectId ("5990faea3268c8e84253ba45"), "url": "stone_7"}

{"_ id": ObjectId ("5990faea3268c8e84253ba46"), "url": "stone_8"}

{"_ id": ObjectId ("5990faea3268c8e84253ba47"), "url": "stone_9"}

{"_ id": ObjectId ("5990faea3268c8e84253ba48"), "url": "stone_10"}

{"_ id": ObjectId ("5990faea3268c8e84253ba49"), "url": "stone_11"}

{"_ id": ObjectId ("5990faea3268c8e84253ba4a"), "url": "stone_12"}

{"_ id": ObjectId ("5990faea3268c8e84253ba4b"), "url": "stone_13"}

{"_ id": ObjectId ("5990faea3268c8e84253ba4c"), "url": "stone_14"}

{"_ id": ObjectId ("5990faea3268c8e84253ba4d"), "url": "stone_15"}

{"_ id": ObjectId ("5990faea3268c8e84253ba4e"), "url": "stone_16"}

Type "it" for more

> it

{"_ id": ObjectId ("5990faea3268c8e84253ba4f"), "url": "stone_17"}

{"_ id": ObjectId ("5990faea3268c8e84253ba50"), "url": "stone_18"}

This is how to add data to the MongoDB shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Database

Wechat

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

12
Report