mongodb 如何在mongoosejs中findAll?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7247541/
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-09-09 12:13:33  来源:igfitidea点击:

How to findAll in mongoosejs?

mongodbmongoose

提问by murvinlai

My code is like that:

我的代码是这样的:

SiteModel.find(
    {},
    function(docs) {
        next(null, { data: docs });
    }
);

but it never returns anything... but if I specify something in the {} then there is one record. so, how to findall?

但它从不返回任何内容……但如果我在 {} 中指定了某些内容,则只有一条记录。那么,如何findall?

回答by pepo

Try this code to debug:

试试这个代码来调试:

SiteModel.find({}, function(err, docs) {
    if (!err) { 
        console.log(docs);
        process.exit();
    }
    else {
        throw err;
    }
});

回答by Tim Knipe

The 2017 Node 8.5 way

2017 Node 8.5 方式

try {
  const results = await SiteModel.find({});
  console.log(results);
} catch (err) {
  throw err;
}

回答by Deeksha Sharma

From the documentation:

文档

let result = SiteModel.find({}, function (err, docs) {});

or using async await you can do like this also:

或者使用 async await 你也可以这样做:

let result = await SiteModel.find({});

回答by Ogala

const result = await SiteModel.find()- Without the {}in the .find()function works as well.

const result = await SiteModel.find()- 没有{}in.find()函数也能工作。