加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码门户网 (https://www.92codes.com/)- 云服务器、云原生、边缘计算、云计算、混合云存储!
当前位置: 首页 > 服务器 > 系统 > 正文

mongoDB中CRUD的使用分析

发布时间:2023-12-17 12:56:35 所属栏目:系统 来源:DaWei
导读: 文章主要给大家分享mongoDB中CRUD内容,很多新手可能对于CRUD不是很了解,因此这篇文章就给大家深入讲讲CRUD,有这方面学习需要的的朋友可以参考一下。

首先,mongoDB,是一
      文章主要给大家分享mongoDB中CRUD内容,很多新手可能对于CRUD不是很了解,因此这篇文章就给大家深入讲讲CRUD,有这方面学习需要的的朋友可以参考一下。

       首先,mongoDB,是一种数据库,但是又区别与mysql,sqlserver、orcle等关系数据库,在优势上面也略高一筹;至于为什么会这么说呢?很简单,我们来举两个例子:
       1.在存储上面,非关系型数据库可以更大规模的存储,打个比方,Facebook用的数据库就是非关系型数据库。
       2.运用起来更加流畅也是这个数据库的优点,将分布式的特点发挥到极致。

db.collection.insert()
       db.collection.insert() 向集合插入一个或多个文档.要想插入一个文档,传递一个文档给该方法;要想插入多个文档,就可以采用该方法。
       例如

db.users.insert(
   [
    { name: "bob", age: 42, status: "A", },
    { name: "ahn", age: 22, status: "A", },
    { name: "xi", age: 34, status: "D", }
   ]
   )
       如果插入成功就会返回

WriteResult({ "nInserted" : 3 })
       如果异常情况,那么就会返回如下咯:

WriteResult({
   "nInserted" : 3,
   "writeConcernError" : {
    "code" : 64,
    "errmsg" : "waiting for replication timed out at shard-a"
   }
   })
       当我们想插入一条数据的时候,采用insert的方法据比较浪费内存,这个时候,我们久采用插入单个的语法db.collection.insertOne() 向集合插入 单个 文档 document 举个小列子来说明一下。

db.users.insertOne(
   {
    name: "sue",
    age: 19,
    status: "P"
   }
   )
       有了单个,就肯定会有多个,那么多个又是怎么样的呢?语法都很类似,db.collection.insertMany()这个语法跟上面没有区别嘛,对不对,当然是错的,你想,如果添加的数据是数组里面嵌套数组,前面两个的方法的性能就大打折扣了,影响数据库的性能。废话少说,列子走一波:

db.users.insertMany(
   [
    {
    _id: 1,
    name: "sue",
    age: 19,
    type: 1,
    status: "P",
    favorites: { artist: "Picasso", food: "pizza" },
    finished: [ 17, 3 ],
    badges: [ "blue", "black" ],
    points: [
     { points: 85, bonus: 20 },
     { points: 85, bonus: 10 }
    ]
    },
    {
    _id: 2,
    name: "bob",
    age: 42,
    type: 1,
    status: "A",
    favorites: { artist: "Miro", food: "meringue" },
    finished: [ 11, 25 ],
    badges: [ "green" ],
    points: [
     { points: 85, bonus: 20 },
     { points: 64, bonus: 12 }
    ]
    },
    {
    _id: 3,
    name: "ahn",
    age: 22,
    type: 2,
    status: "A",
    favorites: { artist: "Cassatt", food: "cake" },
    finished: [ 6 ],
    badges: [ "blue", "Picasso" ],
    points: [
     { points: 81, bonus: 8 },
     { points: 55, bonus: 20 }
    ]
    },
    {
    _id: 4,
    name: "xi",
    age: 34,
    type: 2,
    status: "D",
    favorites: { artist: "Chagall", food: "chocolate" },
    finished: [ 5, 11 ],
    badges: [ "Picasso", "black" ],
    points: [
     { points: 53, bonus: 15 },
     { points: 51, bonus: 15 }
    ]
    },
    {
    _id: 5,
    name: "xyz",
    age: 23,
    type: 2,
    status: "D",
    favorites: { artist: "Noguchi", food: "nougat" },
    finished: [ 14, 6 ],
    badges: [ "orange" ],
    points: [
     { points: 71, bonus: 20 }
    ]
    },
    {
    _id: 6,
    name: "abc",
    age: 43,
    type: 1,
    status: "A",
    favorites: { food: "pizza", artist: "Picasso" },
    finished: [ 18, 12 ],
    badges: [ "black", "blue" ],
    points: [
     { points: 78, bonus: 8 },
     { points: 57, bonus: 7 }
    ]
    }
   ]
   )

       以上就是关于mongoDB中CRUD的用法介绍,本文有很多示例,需要的朋友可以看看,希望文本对大家学习有帮助。

(编辑:源码门户网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章