Javascript Mongoose - exec 函数有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31549857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:48:50  来源:igfitidea点击:

Mongoose - What does the exec function do?

javascriptmongoose

提问by user439133

I came across a piece of Mongoose code that included a query findOne and then an exec() function.

我遇到了一段 Mongoose 代码,其中包括一个查询 findOne 和一个 exec() 函数。

Ive never seen that method in Javascript before? What does it do exactly?

我以前从未在 Javascript 中见过这种方法吗?它具体有什么作用?

回答by danillouz

Basically when using mongoose, documents can be retrieved using helpers. Every model method that accepts query conditions can be executed by means of a callbackor the execmethod.

基本上在使用 mongoose 时,可以使用助手检索文档。每个接受查询条件的模型方法都可以通过 acallbackexec方法执行。

callback:

callback

User.findOne({ name: 'daniel' }, function (err, user) {
  //
});

exec:

exec

User
  .findOne({ name: 'daniel' })
  .exec(function (err, user) {
      //
  });

Therefore when you don't pass a callback you can build a query and eventually execute it.

因此,当您不传递回调时,您可以构建查询并最终执行它。

You can find additional info in the mongoose docs.

您可以在mongoose 文档中找到更多信息。

UPDATE

更新

Something to note when using Promisesin combination with Mongoose async operations is that Mongoose queries are notPromises. Queries do return a thenable, but if you need a realPromise you should use the execmethod. More information can be found here.

Promise与 Mongoose 异步操作结合使用时需要注意的是,Mongoose 查询不是Promise。查询确实返回一个thenable,但是如果你需要一个真正的Promise 你应该使用这个exec方法。可以在此处找到更多信息。

During the update I noticed I didn't explicitly answer the question:

在更新期间,我注意到我没有明确回答这个问题:

Ive never seen that method in Javascript before? What does it do exactly?

我以前从未在 Javascript 中见过这种方法吗?它具体有什么作用?

Well it's nota native JavaScript method, but part of the Mongoose API.

好吧,它不是原生 JavaScript 方法,而是 Mongoose API 的一部分。

回答by Anshul Koka

Daniel has answered this quite beautifully. To elaborate on an exhaustive list of ways to build and execute queries, look at the following use cases:

丹尼尔很好地回答了这个问题。要详细说明构建和执行查询的方法的详尽列表,请查看以下用例:

Query Building

查询构建

Mongoose will not execute a query until thenor exechas been called upon it. This is very useful when building complex queries. Some examples can include using the populateand aggregatefunctions.

Mongoose 不会执行查询,直到thenexec已被调用。这在构建复杂查询时非常有用。一些示例可以包括使用populateaggregate函数。

User.find({name: 'John'}) // Will not execute

Execution via callback

通过回调执行

Although disliked by many due to its nesting nature, queries can be executed by providing the optional callback.

尽管由于其嵌套性质而被许多人不喜欢,但可以通过提供可选的回调来执行查询。

User.find({name: 'John'}, (err, res) => {}) // Will execute

Then API as a Promises/A+

然后 API 作为 Promises/A+

Mongoose queries do provide a thenfunction. This is not to be confused with regular promises. Simply put, the Promises/A+ specification requires a thenfunction to work much like how we're used to with promises.

Mongoose 查询确实提供了一个then功能。这不要与常规承诺混淆。简而言之,Promises/A+ 规范需要一个then函数来工作,就像我们习惯于使用 Promise 一样。

User.find({name: 'John'}).then(); // Will execute
Promise.all([User.find({name: 'John'}), User.find({name: 'Bob'})]) // Will execute all queries in parallel

The exec function

执行函数

From Mongoose docs If you need a fully-fledged promise, use the .exec() function.

来自猫鼬文档 If you need a fully-fledged promise, use the .exec() function.

User.find({name: 'John'}).exec(); // Will execute returning a promise

回答by Alexander Mills

exec()will return a promise if no callback is provided. So the following pattern is very convenient and generic - it can handle callbacks or promises nicely:

exec()如果没有提供回调,将返回一个承诺。所以下面的模式非常方便和通用——它可以很好地处理回调或承诺:

function findAll(query, populate, cb) {

  let q = Response.find(query);

  if (populate && populate.length > 0) {
    q = q.populate(populate);
  }

  // cb is optional, will return promise if cb == null
  return q.lean().exec(cb);

}

I recommend using Bluebird promises with Mongoose, to do that, use this call:

我建议在 Mongoose 中使用 Bluebird 承诺,为此,请使用以下调用:

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');