如何回滚交易或在mongoos中保存信息

时间:2019-09-28 05:25:29

标签: javascript node.js mongodb mongoose

我正在尝试在mongo DB中插入许多内容。所以我的模式是这个

const BlogPost = new Schema({
  id: { type: Number, unique: true },
  empid: String,
  date: Date
});

我有unique ID,所以当我尝试保存某些error occured it save some collections时,如果某些roll back到来,我想error保存所有收藏。

这是我的代码 https://codesandbox.io/s/dreamy-bell-9u0bz

const data = [
      {
        empid: "test123",
        id: 4,
        date: "19-Jul-2019"
      },
      {
        empid: "test13",
        id: 4,
        date: "18-Jul-2019"
      },

      {
        empid: "test13",
        id: 6,
        date: "11-Jul-2019"
      }
    ];

当我尝试保存以上数据it save one collection and then gives error duplicate key时。我想回滚save集合,因此如果发生错误,将不会保存任何集合。 https://codesandbox.io/s/dreamy-bell-9u0bz

0 个答案:

没有答案