关于mongodb的可视化工具:nosql manager for mongodb

在shell(mongodb自带的shell)中指定使用一个数据库:

use user

好像这个工具没啥好说的,就挺好用,就是界面不然jetbrains的DG

关于数据插入:

插入有insertOne和save

db.person.insertOne({document})

在插入时mongodb会有写入的安全级别,
这个安全级别越高,写入越不容易出错,但同时,它消耗的内存会更多

insertOne和save在保存插入数据的时候,如果要保存的数据库不存在,那么就会新建一个

insertOne和save两者的区别:

insertOne如果在插入一条数据时,如果主键已经存在,那么会报错;
而save在保存时,如果主键已存在,它不会报错,而是覆盖

关于ordered:

他是指定数据插入时是否按顺序写入,
ordered:true:

db.person.insertMany(
[{_id:1,name:'zs',age:18},
{_id:1,name:'ls',age:19},
{_id:2,name:'ww',age:20}],
{ordered:true}
)

此时前两个主键是一样的,会报错,只会插入第一条数据的报错信息:

uncaught exception: BulkWriteError({
    "writeErrors" : [
        {
            "index" : 0,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: user.person index: _id_ dup key: { _id: 1.0 }",
            "op" : {
                "_id" : 1,
                "name" : "zs",
                "age" : 18
            }
        }
    ],
    "writeConcernErrors" : [ ],
    "nInserted" : 0,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
}) :
BulkWriteError({
    "writeErrors" : [
        {
            "index" : 0,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: user.person index: _id_ dup key: { _id: 1.0 }",
            "op" : {
                "_id" : 1,
                "name" : "zs",
                "age" : 18
            }
        }
    ],
    "writeConcernErrors" : [ ],
    "nInserted" : 0,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})
BulkWriteError@src/mongo/shell/bulk_api.js:371:48
BulkWriteResult/this.toError@src/mongo/shell/bulk_api.js:336:24
Bulk/this.execute@src/mongo/shell/bulk_api.js:1205:23
DBCollection.prototype.insertMany@src/mongo/shell/crud_api.js:326:5
@(shell):1:2

此时删除一下之前插入的数据:

db.perason.remove()

指定不按顺序插入:
ordered:false:

uncaught exception: BulkWriteError({
    "writeErrors" : [
        {
            "index" : 0,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: user.person index: _id_ dup key: { _id: 1.0 }",
            "op" : {
                "_id" : 1,
                "name" : "zs",
                "age" : 18
            }
        },
        {
            "index" : 1,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: user.person index: _id_ dup key: { _id: 1.0 }",
            "op" : {
                "_id" : 1,
                "name" : "ls",
                "age" : 19
            }
        },
        {
            "index" : 2,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: user.person index: _id_ dup key: { _id: 2.0 }",
            "op" : {
                "_id" : 2,
                "name" : "ww",
                "age" : 20
            }
        }
    ],
    "writeConcernErrors" : [ ],
    "nInserted" : 0,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
}) :
BulkWriteError({
    "writeErrors" : [
        {
            "index" : 0,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: user.person index: _id_ dup key: { _id: 1.0 }",
            "op" : {
                "_id" : 1,
                "name" : "zs",
                "age" : 18
            }
        },
        {
            "index" : 1,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: user.person index: _id_ dup key: { _id: 1.0 }",
            "op" : {
                "_id" : 1,
                "name" : "ls",
                "age" : 19
            }
        },
        {
            "index" : 2,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: user.person index: _id_ dup key: { _id: 2.0 }",
            "op" : {
                "_id" : 2,
                "name" : "ww",
                "age" : 20
            }
        }
    ],
    "writeConcernErrors" : [ ],
    "nInserted" : 0,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})
BulkWriteError@src/mongo/shell/bulk_api.js:371:48
BulkWriteResult/this.toError@src/mongo/shell/bulk_api.js:336:24
Bulk/this.execute@src/mongo/shell/bulk_api.js:1205:23
DBCollection.prototype.insertMany@src/mongo/shell/crud_api.js:326:5
@(shell):1:2

而此时插入了第一,三条数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无名之辈无名之辈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值