【发布时间】:2021-06-30 05:11:39
【问题描述】:
我是 MongoDB/Mongoose 的新手,我正在使用 MongoDB、Node 和 Express 如何从 mongodb 查询和获取数组中的一项?
这是数据库结构
{
"_id" : ObjectId("5e19d76fa45abb5d4d50c1d3"),
"name" : "Leonel Messi",
"country" : "Argentina",
"awards" : [ "Ballon d'Or", "Golden Boot", "FIFA World Player of the Year",]
}
这是在node js环境中使用mongoose和express的查询
router.get('/FindPlayers', async (req, res) => {
const player = await Players.findOne({ name: "Leonel Messi" }, { country: 1, awards: [0], _id: 0, });
res.send(player);
});
我想得到县和奖项数组中的第一项,如下所示
{
country : "Argentina",
awards : [ "Ballon d'Or" ]
}
但是我得到了
{
country : "Argentina",
awards : [ 0 ]
}
【问题讨论】:
标签: node.js arrays mongodb express mongoose