我如何从mongodb数据库获取所有文档?

时间:2019-08-24 01:03:50

标签: javascript node.js async-await

我有一个类,其中包含用于MongoDB Connection的辅助方法,包括connect,findDocuments和insertDocument。

async findDocuments(collection, query) {
        var response = await this.database.collection(collection).find(query).toArray();
        return response;
}

console.log(mongo.findDocuments('users', {}));

我希望获得数据库中所有用户的列表。 我收到一个诺言。

2 个答案:

答案 0 :(得分:1)

异步函数总是返回一个Promise。要查看承诺信息,如果您在函数中,则需要await的结果,如果在全局范围中,则需要使用then()

您的代码看起来像是在全局范围内,因此您需要使用then:

class Mongo {
  async findDocuments(collection, query) {
    var response = (await this.database.collection(collection).find(query)).toArray();
    return response;
  }
}

let mongo = new Mongo();

mongo.findDocuments('users', {}).then(result => {
  console.log(result);
});

答案 1 :(得分:0)

Line number: 1 - NOT NULL constraint failed: mco_voterstest.zipcode_id , Waggoner, Tempe, 888888888, 2006-10-05, 9999, S McClintock Dr, 85284 Traceback (most recent call last): File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute return Database.Cursor.execute(self, query, params) sqlite3.IntegrityError: NOT NULL constraint failed: mco_voterstest.zipcode_id The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/import_export/resources.py", line 522, in import_row self.save_instance(instance, using_transactions, dry_run) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/import_export/resources.py", line 315, in save_instance instance.save() File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/models/base.py", line 741, in save force_update=force_update, update_fields=update_fields) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/models/base.py", line 779, in save_base force_update, using, update_fields, File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/models/base.py", line 870, in _save_table result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/models/base.py", line 908, in _do_insert using=using, raw=raw) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _insert return query.get_compiler(using=using).execute_sql(return_id) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1335, in execute_sql cursor.execute(sql, params) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute return super().execute(sql, params) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers return executor(sql, params, many, context) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/Users/lawrencemhudson/Documents/goptest/testenv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: NOT NULL constraint failed: mco_voterstest.zipcode_id 返回的游标不是promise,因此您可以正确调用它。之所以得到承诺,是因为您没有等待对findDocuments的调用。

find

这还假设您也在异步函数中调用此函数。

节点驱动程序参考:

http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#find