Node.js Mongoose异步/等待查找然后编辑并保存?

Node.js Mongoose异步/等待查找然后编辑并保存?,node.js,mongodb,asynchronous,mongoose,Node.js,Mongodb,Asynchronous,Mongoose,是否可以使用异步/等待承诺进行查找然后保存 我有以下代码: try { var accounts = await Account.find() .where("username").in(["email@gmail.com"]) .exec(); accounts.password = 'asdf'; accounts.save(); } catch (error) { handleError(res, error.message); } 我得到

是否可以使用异步/等待承诺进行查找然后保存

我有以下代码:

try {
    var accounts = await Account.find()
    .where("username").in(["email@gmail.com"])
    .exec();
    accounts.password = 'asdf';
    accounts.save();
} catch (error) {
    handleError(res, error.message);
}
我得到了以下错误:

ERROR: accounts.save is not a function

这就是我想要的:

try {
    var accounts = await Account.findOneAndUpdate(
        {"username" : "helllo@hello.com"},
        {$set: {"password" : "aaaa"}},
        {new : true}
    );
    res.status(200).json(accounts);
} catch (error) {
    handleError(res, error.message);
}
或者(感谢@johnyhk提供的find vs findOne提示!)


accounts
是找到的文档数组,所以您的代码实际上并没有编辑任何内容。你想在这里做什么?@johnyhk我想我只是在玩弄猫鼬、查询和承诺(以等待/同步格式)。我猜上面的代码没有意义。如果我想找到所有用户名为
hello@hello.com
并将密码更改为
asdf
?我将更改上面的代码以反映这个问题。这仍然没有任何意义,因为
帐户
仍然是一个数组。首先使用
findOne
而不是
find
,这样会更有意义。@johnyhk谢谢!你把我带到了我需要的地方。现在更有意义了。
try {
    var accounts = await Account.findOne()
    .where("username").in(["hello@hello.com"])
    .exec();
    accounts.password = 'asdf';
    accounts.save();
    res.status(200).json(accounts);
} catch (error) {
    handleError(res, error.message);
}