如何mongoose.connect(connectionSring)工作?
连接到数据库时,没有一个ORM执行的SQL我来自一个Python背景。与Python库cx_Oracle
,像这样说:
>>> conn = cx_Oracle.connect(connectionString)
>>> curs = conn.cursor()
>>> _ = curs.execute(...)
更具体地讲,通过返回的连接对象conn
指导我所有的电话到数据库中,而不是库cx_Oracle
本身。
在express
应用程序,使用MongoDB的具有node.js
和mongoose
,我们可能会做这样的事情:
require('./models/user'); // Defines new Schema in mongoose named 'users'
require('./services/passport'); // receives data from OAuth flow
// and writes new authenticated users to MongoDB database
mongoose.connect(keys.mongoURI, {useNewUrlParser: true});
const app = express();
require('./routes/authRoutes')(app); // handle OAuth routes and pass to passport authentication
// server runs and listens to routes etc
好像mongoose
本身是越来越设置与新属性导入库:
mongoose.connect(keys.mongoURI, {useNewUrlParser: true});
像在./services/passport.js
下面的后续调用创建新用户没有明显的参考我们的连接。
const User = mongoose.model('users');
// within an OAuth callback
new User({ id: response.data.id })
.save()
.then(...)
我看了看源Mongoose.prototype.connect
明白这一点,但我通过return语句混淆。承诺完成后,将返回箭头功能与_mongoose
,我们对原型Mongoose
本身有一个新的连接的情况下,但我们没有返回我们的应用程式内容。
Mongoose.prototype.connect = function() {
const _mongoose = this instanceof Mongoose ? this : mongoose;
const conn = _mongoose.connection;
return conn.openUri(arguments[0], arguments[1], arguments[2]).then(() => _mongoose);
};
有人可以请解释正在发生的事情对我们的进口mongoose
库时,我们称之为mongoose.connect(...)
?或许把一些资源,这样我就可以看到这样一个简单的例子?谢谢。
猫鼬会尝试与MongoClient内部连接,并返回猫鼬的实例。
const promise = new Promise((resolve, reject) => {
const client = new mongodb.MongoClient(uri, options);
_this.client = client;
client.connect(function(error) {
if (error) {
_this.readyState = STATES.disconnected;
return reject(error);
}
const db = dbName != null ? client.db(dbName) : client.db();
_this.db = db;
}
这是“conn.openUri”功能和猫鼬将执行功能的内部处理。您也可以直接连接mongoClient不使用猫鼬。
https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html
如何mongoose.connect(connectionSring)工作?
连接到数据库时,没有一个ORM执行的SQL我来自一个Python背景。与Python库cx_Oracle
,像这样说:
>>> conn = cx_Oracle.connect(connectionString)
>>> curs = conn.cursor()
>>> _ = curs.execute(...)
更具体地讲,通过返回的连接对象conn
指导我所有的电话到数据库中,而不是库cx_Oracle
本身。
在express
应用程序,使用MongoDB的具有node.js
和mongoose
,我们可能会做这样的事情:
require('./models/user'); // Defines new Schema in mongoose named 'users'
require('./services/passport'); // receives data from OAuth flow
// and writes new authenticated users to MongoDB database
mongoose.connect(keys.mongoURI, {useNewUrlParser: true});
const app = express();
require('./routes/authRoutes')(app); // handle OAuth routes and pass to passport authentication
// server runs and listens to routes etc
好像mongoose
本身是越来越设置与新属性导入库:
mongoose.connect(keys.mongoURI, {useNewUrlParser: true});
像在./services/passport.js
下面的后续调用创建新用户没有明显的参考我们的连接。
const User = mongoose.model('users');
// within an OAuth callback
new User({ id: response.data.id })
.save()
.then(...)
我看了看源Mongoose.prototype.connect
明白这一点,但我通过return语句混淆。承诺完成后,将返回箭头功能与_mongoose
,我们对原型Mongoose
本身有一个新的连接的情况下,但我们没有返回我们的应用程式内容。
Mongoose.prototype.connect = function() {
const _mongoose = this instanceof Mongoose ? this : mongoose;
const conn = _mongoose.connection;
return conn.openUri(arguments[0], arguments[1], arguments[2]).then(() => _mongoose);
};
有人可以请解释正在发生的事情对我们的进口mongoose
库时,我们称之为mongoose.connect(...)
?或许把一些资源,这样我就可以看到这样一个简单的例子?谢谢。
猫鼬会尝试与MongoClient内部连接,并返回猫鼬的实例。
const promise = new Promise((resolve, reject) => {
const client = new mongodb.MongoClient(uri, options);
_this.client = client;
client.connect(function(error) {
if (error) {
_this.readyState = STATES.disconnected;
return reject(error);
}
const db = dbName != null ? client.db(dbName) : client.db();
_this.db = db;
}
这是“conn.openUri”功能和猫鼬将执行功能的内部处理。您也可以直接连接mongoClient不使用猫鼬。
https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html