我正在使用MongoDB Atlas cloud(https://cloud.mongodb.com/)和Mongoose库。
我尝试使用事务处理概念创建多个文档,但是它不起作用。我没有任何错误。但是,回滚似乎无法正常工作。
app.js
//*** more code here
var app = express();
require('./models/db');
//*** more code here
Run Code Online (Sandbox Code Playgroud)
模型/db.js
var mongoose = require( 'mongoose' );
// Build the connection string
var dbURI = 'mongodb+srv://mydb:pass@cluster0-****.mongodb.net/mydb?retryWrites=true';
// Create the database connection
mongoose.connect(dbURI, {
useCreateIndex: true,
useNewUrlParser: true,
});
// Get Mongoose to use the global promise library
mongoose.Promise = global.Promise;
Run Code Online (Sandbox Code Playgroud)
型号/user.js
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
userName: {
type: String,
required: true
},
pass: {
type: …Run Code Online (Sandbox Code Playgroud)