When executing a query what a callback function should be
来源:互联网 发布:vb贪吃蛇代码 编辑:程序博客网 时间:2024/06/05 16:51
摘自mongoose官网
Queries
Documents can be retrieved through several static helper methods of models.
Any model method which involves specifying query conditions can be executed two ways:
When a callback
function:
- is passed, the operation will be executed immediately with the results passed to the callback.
- is not passed, an instance of Query is returned, which provides a special query builder interface.
In mongoose 4, a Query has a .then()
function, and thus can be used as a promise.
When executing a query with a callback
function, you specify your query as a JSON document. The JSON document's syntax is the same as the MongoDB shell.
var Person = mongoose.model('Person', yourSchema);// find each person with a last name matching 'Ghost', selecting the `name` and `occupation` fieldsPerson.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) { if (err) return handleError(err); console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host.})
Here we see that the query was executed immediately and the results passed to our callback. All callbacks in Mongoose use the pattern: callback(error, result)
. If an error occurs executing the query, the error
parameter will contain an error document, and result
will be null. If the query is successful, the error
parameter will be null, and the result
will be populated with the results of the query.
Anywhere a callback is passed to a query in Mongoose, the callback follows the patterncallback(error, result)
. What result
is depends on the operation: For findOne()
it is a potentially-null single document, find()
a list of documents, count()
the number of documents, update()
thenumber of documents affected, etc. The API docs for Models provide more detail on what is passed to the callbacks.
- When executing a query what a callback function should be
- expreiment to see what a callback function should be When executing a query
- What should we do when meet a crash in android?
- What should we do when meet a crash in android
- Friend function should be a public function, if it is in a class.
- What is a callback api
- what should you be as a programmer?To be the last one and become experienced!
- org.hibernate.QueryException: addEntity() or addScalar() must be called on a sql query before executing the query 问题的解决
- org.hibernate.QueryException: addEntity() or addScalar() must be called on a sql query before executing the query
- The "data" option should be a function that returns a per-instance value in component definitions.
- Executing a Method When a Bean Is Destroyed
- The function default must be used with a prefix when a default namespace is not specified
- The function getXX must be used with a prefix when a default namespace is not specified
- The function size must be used with a prefix when a default namespace is not specified
- The function startsWith must be used with a prefix when a default namespace
- The function equals must be used with a prefix when a default namespace is not specified
- The function size must be used with a prefix when a default namespace is not specified
- 解决The function contains must be used with a prefix when a default namespace is
- 异步调用和回调函数
- 程序猿的小情人
- 2 操作臂的运行学
- 同步调用与异步调用
- ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/xcodeproject
- When executing a query what a callback function should be
- Win访问Linux samba拒绝访问
- 图上轮播
- 追溯源码解决android疑难问题1
- Dubbo入门基础与实例讲解
- 关于异步的思考
- Elixir 函数式编程语言
- php函数变量引用
- linux木马中毒