美文网首页
mongodb插入数据时ordered属性影响

mongodb插入数据时ordered属性影响

作者: nextliving | 来源:发表于2018-07-10 14:21 被阅读17次

mongodb中可以使用insertOne和insertMany插入数据,本文主要演示insertMany插入,并且比较ordered属性为true和false时插入含有key(_id)重复的数据的区别。

连接mongodb数据库

在进行下面的操作前需要先使用mongo shell连接mongodb数据库,参考启动mongod并使用mongo shell连接

插入数据

后面将数据插入到video数据库中,因此执行use video先切换到该数据库:

switched to db video

然后执行下面的命令插入数据:

db.moviesScratch.insertMany(
    [
        {
        "_id" : "tt0084726",
        "title" : "Star Trek II: The Wrath of Khan",
        "year" : 1982,
        "type" : "movie"
          },
          {
        "_id" : "tt0796366",
        "title" : "Star Trek",
        "year" : 2009,
        "type" : "movie"
          },
          {
        "_id" : "tt0084726",
        "title" : "Star Trek II: The Wrath of Khan",
        "year" : 1982,
        "type" : "movie"
          },
          {
        "_id" : "tt1408101",
        "title" : "Star Trek Into Darkness",
        "year" : 2013,
        "type" : "movie"
          },
          {
        "_id" : "tt0117731",
        "title" : "Star Trek: First Contact",
        "year" : 1996,
        "type" : "movie"
        }
    ]
);

执行结果为

2018-07-10T14:07:17.525+0800 E QUERY    [thread1] BulkWriteError: write error at item 2 in bulk operation :
BulkWriteError({
    "writeErrors" : [
        {
            "index" : 2,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: video.moviesScratch index: _id_ dup key: { : \"tt0084726\" }",
            "op" : {
                "_id" : "tt0084726",
                "title" : "Star Trek II: The Wrath of Khan",
                "year" : 1982,
                "type" : "movie"
            }
        }
    ],
    "writeConcernErrors" : [ ],
    "nInserted" : 2,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})
BulkWriteError@src/mongo/shell/bulk_api.js:369:48
BulkWriteResult/this.toError@src/mongo/shell/bulk_api.js:333:24
Bulk/this.execute@src/mongo/shell/bulk_api.js:1177:1
DBCollection.prototype.insertMany@src/mongo/shell/crud_api.js:314:5
@(shell):1:1

可以看到nInserted值为2,也就是只插入了最前面2条数据。这是因为第3条数据的_id和第1条数据的_id完全一样,触发了duplicate key error,导致后面不再执行。此时插入数据的方式为有序插入,有个属性ordered值为true.该值不指定时默认为true.如果要让其它不重复的值都能被插入,需要指定ordered值为false.

指定ordered为false插入数据

首先执行db.moviesScratch.remove({})清空集合中的数据:

WriteResult({ "nRemoved" : 2 })

然后执行下面的命令重新插入数据:

db.moviesScratch.insertMany(
    [
        {
        "_id" : "tt0084726",
        "title" : "Star Trek II: The Wrath of Khan",
        "year" : 1982,
        "type" : "movie"
        },
        {
        "_id" : "tt0796366",
        "title" : "Star Trek",
        "year" : 2009,
        "type" : "movie"
        },
        {
        "_id" : "tt0084726",
        "title" : "Star Trek II: The Wrath of Khan",
        "year" : 1982,
        "type" : "movie"
        },
        {
        "_id" : "tt1408101",
        "title" : "Star Trek Into Darkness",
        "year" : 2013,
        "type" : "movie"
        },
        {
        "_id" : "tt0117731",
        "title" : "Star Trek: First Contact",
        "year" : 1996,
        "type" : "movie"
        }
    ],
    {
        "ordered": false 
    }
);

这个命令和上一个插入命令的区别是加入了{ "ordered": false }。执行以后输出:

2018-07-10T14:17:06.012+0800 E QUERY    [thread1] BulkWriteError: write error at item 2 in bulk operation :
BulkWriteError({
    "writeErrors" : [
        {
            "index" : 2,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: video.moviesScratch index: _id_ dup key: { : \"tt0084726\" }",
            "op" : {
                "_id" : "tt0084726",
                "title" : "Star Trek II: The Wrath of Khan",
                "year" : 1982,
                "type" : "movie"
            }
        }
    ],
    "writeConcernErrors" : [ ],
    "nInserted" : 4,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})
BulkWriteError@src/mongo/shell/bulk_api.js:369:48
BulkWriteResult/this.toError@src/mongo/shell/bulk_api.js:333:24
Bulk/this.execute@src/mongo/shell/bulk_api.js:1177:1
DBCollection.prototype.insertMany@src/mongo/shell/crud_api.js:314:5
@(shell):1:1

可以看到这次插入了4条数据。虽然也有duplicate key error,但是该命令能保证不重复的key都能被插入。

相关文章

网友评论

      本文标题:mongodb插入数据时ordered属性影响

      本文链接:https://www.haomeiwen.com/subject/msvfpftx.html