[英]Mongoose return all array data
I am working in a nodejs rest api using mongodb and want return all array data from the next structure我正在使用 mongodb 在 nodejs rest api 中工作,并希望从下一个结构返回所有数组数据
[
{
"inventorydata": [
[
{
"location": "principal",
"code": "123",
"description": "prueba 1"
},
{
"location": "secundaria",
"code": "456",
"description": "prueba 2"
},
{
"location": "tercera",
"code": "789",
"description": "prueba 3"
}
]
],
"_id": "5fff69999119ae1a049955d3",
"inventory_code": "ed9825f7-647b-443d-8e4c-69acf30cb292",
"createdAt": "2021-01-13T21:43:53.318Z",
"__v": 0
}
]
I am trying to return all items from inventoryData using the following code inside nodejs我正在尝试使用nodejs中的以下代码从inventoryData返回所有项目
app.post('/get', async (req, res) => {
try {
const item = await imgModel.find({ 'inventorydata' : { $elemMatch: { } } });
res.json(item)
} catch (e) {
res.json({ error_code: 1, err_desc: "Corupted excel file" });
}
});
This is the expected output for the nodejs response这是 nodejs 响应的预期 output
[
{
"location": "principal",
"code": "123",
"description": "prueba 1"
},
{
"location": "secundaria",
"code": "456",
"description": "prueba 2"
},
{
"location": "tercera",
"code": "789",
"description": "prueba 3"
}
]
You can use projection to return values you want.您可以使用投影来返回您想要的值。
Also using mongoose
you can access your data using JS in this way:还使用
mongoose
您可以通过以下方式使用 JS 访问您的数据:
yourModel.find({/*your_query*/},{"inventorydata": 1}).then(result => {
console.log("inventorydata = ",result.inventorydata)
}).catch(e => {
// error
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.