MongoDB中怎么实现数据增加操作

45次阅读
没有评论

共计 2167 个字符,预计需要花费 6 分钟才能阅读完成。

自动写代码机器人,免费开通

这期内容当中丸趣 TV 小编将会给大家带来有关 MongoDB 中怎么实现数据增加操作,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

只要是数据库那么就绝对离不开最为核心的功能:CRUD,所以在 MongoDB 里面对于数据的操作也是支持的,但是需要提醒的是,除了增加之外,其它的都很麻烦。

1、数据增加
使用“db. 集合.insert()”可以实现数据的增加操作。
范例:增加一个简单数据
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 }

范例:保存数组
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 }
如果想要保存多个数据,那么就使用数组。

范例:保存 1000 个数据
for(var x=0;x 1000;x++){db.info.insert({ url : stone_ +x});}
WriteResult({nInserted : 1})
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 }

上述就是丸趣 TV 小编为大家分享的 MongoDB 中怎么实现数据增加操作了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注丸趣 TV 行业资讯频道。

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-04发表,共计2167字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)