我想将数据存储在节点中的两个不同的数据库中。我很累但无法正常工作。
// connect two databases
mongoose.connect(
keys.mongoURI
);
mongoose.connect(
keys.mongoURI1 //connectivity link
);
const express = require('express');
const app = express();
const ServerPortRouter = express.Router();
const ServerPort = require('../models/ServerPort');
ServerPortRouter.route('/add').post(function (req, res) {
const serverport = new ServerPort(req.body);
serverport.save()
.then(serverport => {
res.json('Server added successfully');
})
});
数据存储在单个数据库中,但是我不知道将相同的数据存储在两个不同的数据库中
我开始感到疲倦,但我做不到。有帮助吗?
答案 0 :(得分:0)
尝试如下为每个连接/数据库创建单独的模型:
var conn = mongoose.createConnection('mongodb://localhost/testA');
var conn2 = mongoose.createConnection('mongodb://localhost/testB');
// stored in 'testA' database
var ModelA = conn.model('Model', new mongoose.Schema({
title : { type : String, default : 'model in testA database' }
}));
// stored in 'testB' database
var ModelB = conn2.model('Model', new mongoose.Schema({
title : { type : String, default : 'model in testB database' }
}));