在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
而此时插入了第一,三条数据