javascript - 我的 api 代码从 mongodb 检索一个空数据数组,而该代码在 mongodb 操场上运行良好
问题描述
我的 mongodb 数据库包含一组用户,每个用户都有一系列商店,每个商店都有一系列产品。这是我的集合结构的简化版本:
[
{
"_id": "60e66b70da2439232e330415",
"name": "User1 Name",
"shops": [
{
"_id": "60e7c9e2be0d8f03544a03b8",
"shopName": "User1 Shop1 Name",
"products": [
{
"_id": "60e9e9e8105d6021a2e91535",
"title": "User1 Shop1 Product1 Title"
},
{
"_id": "60e9f4a0105d6021a2e91536",
"title": "User1 Shop1 Product2 Title"
}
]
},
{
"_id": "60e8e8c00f3986577cb968c9",
"shopName": "User1 Shop2 Name",
"products": [
{
"_id": "60e9f4fe105d6021a2e91537",
"title": "User1 Shop2 Product1 Title"
},
{
"_id": "60e9f515105d6021a2e91538",
"title": "User1 Shop2 Product2 Title"
}
]
}
]
},
{
"_id": "60e66b93da2439232e330416",
"name": "User2 Name",
"shops": [
{
"_id": "60e69698e76cad44e49e1fc8",
"shopName": "User2 Shop1 Name",
"products": [
{
"_id": "60e9f588105d6021a2e91539",
"title": "User2 Shop1 Product1 Title"
},
{
"_id": "60e9f59c105d6021a2e9153a",
"title": "User2 Shop1 Product2 Title"
}
]
},
{
"_id": "60e902b441e9df63c7fbcb49",
"shopName": "User2 Shop2 Name",
"products": [
{
"_id": "60e9f5c9105d6021a2e9153b",
"title": "User2 Shop2 Product1 Title"
},
{
"_id": "60e9f5de105d6021a2e9153c",
"title": "User2 Shop2 Product2 Title"
}
]
}
]
}
]
我有一个 api 端点,例如.../api/products/60e9f5de105d6021a2e9153c
. 此端点包括一个参数,它是一个 productId。我有以下两个代码从我的 mongodb 集合中检索产品数据,但检索到的数组为空。
我的端点代码:
app.get("/api/products/:productId", (req,res)=>{
let productId = req.params.productId;
myData.getProductByProductId(productId)
.then(products => res.json(products))
.catch(err => res.json({"message": err}));
});
我的数据服务代码:
getProductByProductId: function (productId) {
return new Promise((resolve, reject) => {
User.aggregate([
{
$match: {
"shops.products._id": productId
}
},
{
"$unwind": "$shops"
},
{
"$unwind": "$shops.products"
},
{
$match: {
"shops.products._id": productId
}
},
{
$project: {
"_id": "$shops.products._id",
"title": "$shops.products.title"
}
}
])
.exec().then(products => {
resolve(products)
}).catch(err => {
reject(err);
});
});
}
聚合代码在这个操场上运行良好,但在我的网络应用程序代码中检索一个空数组。
解决方案
原来 ObjectId 的铸造似乎是问题所在。我们需要使用mongoose.Types.ObjectId
API 代码将如下所示
getProductByProductId: function (productId) {
return new Promise((resolve, reject) => {
User.aggregate([
{
$match: {
"shops.products._id": mongoose.Types.ObjectId(productId)
}
},
{
"$unwind": "$shops"
},
{
"$unwind": "$shops.products"
},
{
$match: {
"shops.products._id": mongoose.Types.ObjectId(productId)
}
},
{
$project: {
"_id": "$shops.products._id",
"title": "$shops.products.title"
}
}
])
.then(products => {
resolve(products)
}).catch(err => {
reject(err);
});
});
}
推荐阅读
- laravel - 没有数据通过表单保存到我的数据库中
- java - 在 forge 1.8.9 mod 中加载纹理模型时出现问题
- java - Spring Boot HttpSecurity - @PreAuthorize - 如何设置 AuthenticationFilter?
- c - 我正在尝试在 atmega164 上制作一个简单的吉他调音器,但我无法制作用于在 CodeVisionAVR 上读取频率的代码
- javascript - 如何在 javascript 中强制 Date.parse() 转换为本地时间和日期而不是 UTC
- ssis - SSIS 脚本任务不填充 SSAS 表格模型属性的数据
- flutter - 如何在没有 Renderflex 溢出错误的情况下动态更改 Container/PageView 的大小
- swagger - 将所有标题定义为单个字典
- python - 无法在生产环境中访问烧瓶中的自定义 HTTP 标头(NGINX + Gunicorn3)
- javascript - JavaScript:根据给定的范围和集合生成唯一的“x”数字