我在Ubuntu上使用Mongoose 4.12.4和Mongo v3.4.9运行Node v6.11.4。我有以下代码来添加新记录,然后回读以下内容。保存未执行(fluffy.save()
)。
但是,如果我使用mongoose.connect将mongoose.createConnection
行替换为它上面的两行,那么整个代码将按预期工作。
// getting-started.js
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var options = {promiseLibrary: require('bluebird')};
//mongoose.connect('mongodb://localhost/test',options);
//var db = mongoose.connection;
var db = mongoose.createConnection('mongodb://localhost/test', options);
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('We\'re Connected!');
var kittySchema = mongoose.Schema({
name: String
});
// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't have a name";
console.log(greeting);
}
var Kitten = mongoose.model('Kitten', kittySchema);
var silence = new Kitten({name: 'Silence'});
console.log(silence.name); // 'Silence'
var fluffy = new Kitten({name: 'fluffy'});
fluffy.speak(); // "Meow name is fluffy"
fluffy.save(function (err, fluffy) {
if (err)
return console.error(err);
fluffy.speak();
}).then((val) => {
console.log("result: " + val);
Kitten.find({name: 'fluffy'}, function (err, kittens) {
if (err)
return console.error(err);
console.log("All the kittens found: " + kittens);
})
}
).catch((err) => console.log("rejected:" + err));
});
使用mongoose.createConnection的结果:
We're Connected!
Silence
Meow name is fluffy
使用mongo.connect:
更新代码的结果We're Connected!
Silence
Meow name is fluffy
result: { __v: 0, name: 'fluffy', _id: 59ed75e7c414e210bc074bf3 }
Meow name is fluffy
All the kittens found: { _id: 59ed635dbcefe203f07a4b35, name: 'fluffy', __v: 0 },{ _id: 59ed63bddfb1de04351704e5, name: 'fluffy', __v: 0 },{ _id: 59ed63ee6c7ca0046872ff4e, name: 'fluffy', __v: 0 },{ _id: 59ed6f5fc816670d3bc0a09e, name: 'fluffy', __v: 0 },{ _id: 59ed6fd49319d20d8944b7e2, name: 'fluffy', __v: 0 },{ _id: 59ed74785885cf0ff23ef0ee, name: 'fluffy', __v: 0 },{ _id: 59ed75e7c414e210bc074bf3, name: 'fluffy', __v: 0 }
答案 0 :(得分:1)
您可以通过以下方式处理连接:
const mongoose = require("mongoose"),
dbURI = "mongodb://your-host(ex - localhost):your port (ex - 27017)/yourDatabaseName";
mongoose.connect(dbURI);
conn = mongoose.connection;
conn.on("connected", function(){
console.log("Mongoose connected to " + dbURI);
});
conn.on("error", function(err){
console.log("Mongoose connection error" + err);
});
conn.on("disconnected", function(){
console.log("Mongoose disconnected");
});
答案 1 :(得分:1)
基于Queries hang when using mongoose.createConnection() vs mongoose.connect()中的答案,我们在使用createConnection()时必须使用db.model()而不是mongoose.model()。 mongoose.model()只能与connect()一起使用。我已经粘贴了两个有效的解决方案。
使用connect()的解决方案:
var mongoose = require('mongoose');
var util = require('util');
mongoose.Promise = require('bluebird');
var options = {promiseLibrary: require('bluebird'), useMongoClient: true};
var promise = mongoose.connect('mongodb://localhost/test', options);
promise.then(function (db) {
console.log('We\'re Connected!');
var kittySchema = mongoose.Schema({
name: String
});
// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't have a name";
console.log(greeting);
}
// NOTE: Use mongoose.model() instead of db.model() when using connect().
var Kitten = mongoose.model('Kitten', kittySchema);
var fluffy = new Kitten({name: 'fluffy'});
fluffy.speak(); // "Meow name is fluffy"
fluffy.save(function (err, fluffy) {
if (err)
return console.error(err);
fluffy.speak();
})
.then((val) => {
console.log("result: " + val);
Kitten.find({name: 'fluffy'}, function (err, kittens) {
if (err) {
return console.error(err);
}
console.log("All the kittens found: " + kittens);
})
}
)
.catch((err) => {
console.log("Mongo save rejected:" + err);
process.exit(1);
}
);
});
使用createConnection()解决方案:
var mongoose = require('mongoose');
var util = require('util');
mongoose.Promise = require('bluebird');
var options = {promiseLibrary: require('bluebird'), useMongoClient: true};
var promise = mongoose.createConnection('mongodb://localhost/test', options);
promise.then(function (db) {
var kittySchema = mongoose.Schema({
name: String
});
// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't have a name";
console.log(greeting);
}
// NOTE use db.model() instead of mongoose.model when using mongoose.createConnection()
var Kitten = db.model('Kitten', kittySchema);
var fluffy = new Kitten({name: 'fluffy'});
fluffy.speak(); // "Meow name is fluffy"
fluffy.save(function (err, fluffy) {
if (err)
return console.error(err);
fluffy.speak();
})
.then((val) => {
console.log("result: " + val);
Kitten.find({name: 'fluffy'}, function (err, kittens) {
if (err) {
return console.error(err);
}
console.log("All the kittens found: " + kittens);
})
})
.catch((err) => {
console.log("Mongo save rejected:" + err);
process.exit(1);
}
);
})
.catch((err) => {
console.error(`Error connecting to MongoDB: ${err}`);
process.exit(1);
}
);
promise.on("disconnected", function () {
console.log("Mongoose disconnected");
process.exit(1);
});
promise.on('err', function () {
console.error(`Error connecting to MongoDB: ${err}`);
process.exit(1);
});