Node.js 具有async Wait的findOneAndUpdate不返回新文档
下面的代码正在更新数据库,但没有返回新文档,尽管特别声明“returnNewDocument”为true。我没有使用mongoose,而是使用这里的MongoDB驱动程序 对于mongo,您必须显式地提供returnNewDocument:true,因为默认情况下,它会在更新之前返回旧文档Node.js 具有async Wait的findOneAndUpdate不返回新文档,node.js,mongodb,Node.js,Mongodb,下面的代码正在更新数据库,但没有返回新文档,尽管特别声明“returnNewDocument”为true。我没有使用mongoose,而是使用这里的MongoDB驱动程序 对于mongo,您必须显式地提供returnNewDocument:true,因为默认情况下,它会在更新之前返回旧文档 如果您使用相同的东西,但您只需要提供新的:在第三个对象中为true。第一个是查找查询,第二个是更新查询,第三个是额外选项您使用什么库进行mongo查询?有些库可能有自己的操作生成器,所以他们可能不希望返回Ne
如果您使用相同的东西,但您只需要提供新的:在第三个对象中为true。第一个是查找查询,第二个是更新查询,第三个是额外选项您使用什么库进行mongo查询?有些库可能有自己的操作生成器,所以他们可能不希望返回NewDocument,而是像mongoose new..等。那么检查图书馆文档这是否回答了你的问题?不幸的是,它引用的是节点驱动程序,而不是这里找到的MongoDB驱动程序{new:true}删除upsert true和returnNew document只是添加到上面的行我认为new:true是mongoose的东西我确实设置了returnNewDocument:true,但我没有得到新文档
const { MongoClient } = require("mongodb");
...
const mongo = new MongoClient(process.env.MONGODB_CONNECTION_URI, {
useUnifiedTopology: true
});
if (mongo) {
mongo.connect();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "build")));
app.listen(8080, () => console.log("Server running on 8080"));
}
app.get("/v1/editprofile", function(req, res) {
const { givenName, familyName, mobile, address, serviceRadius, language } = req.query;
if (req.cookies && req.cookies.participant) {
jwt.verify(req.cookies.participant, privateKey, { algorithms: ["RS256"] }, function(err, decoded) {
if (err) res.status(400).send({ status: "logout", description: "Bad Cookie found" });
async function insertProfile() {
const id = decoded.id;
const data = await mongo
.db("Users")
.collection("participants")
.findOneAndUpdate(
{ id },
{
$set: {
givenName,
familyName,
mobile,
address,
serviceRadius,
language
}
},
{
upsert: true,
returnNewDocument: true
}
);
if (data) {
const participant = data.value
console.log(participant)
res.status(200).send({ participant });
}
}
insertProfile().catch(console.error);
});
} else {
res.status(400).send({ status: "logout", description: "No Cookie found" });
}
});