最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何mongoose.connect(connectionSring)工作?

IT培训 admin 2浏览 0评论

如何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.jsmongoose,我们可能会做这样的事情:

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.jsmongoose,我们可能会做这样的事情:

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
发布评论

评论列表 (0)

  1. 暂无评论