gpt4 book ai didi

node.js - Mongoose如何找到特定值

转载 作者:太空宇宙 更新时间:2023-11-03 23:01:22 24 4
gpt4 key购买 nike

读了很多书后,我陷入困境。我在这里发布的代码是我试图制作的商店数据库的实现。在每家商店,我们都有一些字段。我有兴趣对包含 JSON 变量的 items 数组执行一些操作。我想通过三个过滤器来过滤商品,首先是商店 ID,其次是类别 ID,最后一个过滤器是半类别 ID。

我想从前端发送数据,这意味着我提供 STOREIDCategoryIDSemiCategoryID。在后端接收到数据后,我希望根据前端提供的数据只收到相关的项目。

{
"_id": {
"$oid": "5a1844b5685cb50a38adf5bb" --> **ID of the STORE**
},
"name": "ACE",
"user_id": "59e4c41105d1f6227c1771ea",
"imageURL": "none",
"rating": 5,
"items": [
{
"name": "NirCohen",
"categoryID": "5a0c2d292235680012bd12c9",
"semiCatID": "5a0c2d5a2235680012bd12ca",
"_id": {
"$oid": "5a1958181cd8a208882a80f9"
}
},
{
"name": "he",
"categoryID": "5a0c2d292235680012bd12c9",
"semiCatID": "5a0c2d5a2235680012bd12ca",
"_id": {
"$oid": "5a1973c40e561e08b8aaf2b2"
}
},
{
"name": "a",
"categoryID": "5a0c2d292235680012bd12c9",
"semiCatID": "5a0c2d5a2235680012bd12ca",
"_id": {
"$oid": "5a197439bc1310314c4c583b"
}
},
{
"name": "aaa",
"categoryID": "5a0c2d292235680012bd12c9",
"semiCatID": "5a0c2d5a2235680012bd12ca",
"_id": {
"$oid": "5a197474558a921bb043317b"
}
},
],
"__v": 9
}

我希望后端根据查询返回过滤后的项目。问题是,我无法获得正确的查询。

非常感谢您的帮助,

提前谢谢您。

最佳答案

如果我理解正确的话,你正在做这样的事情:

Store.find({}).then(..);

如果您只想查找categoryID等于变量myCategory的商店,您可以使用以下方法过滤掉它们:

Store.find({semiCatID: myCategory}).then(..);

如果这不是您想要的,请告诉我,然后我们可以继续努力共同解决这个问题。

编辑:因此,您将从前端发送变量 StoreIDCategoryIDSemiCategoryID。在后端接收它们,并想要过滤匹配所有三个字段的数据库集合?如果是这样..那么我认为您所要做的就是更改当前的查询:

store.findOne({ _id: req.body.userID }, (err, store) => { console.log(store); }); 

类似于:

store.findOne({
_id: req.body.userID,
storeID: req.body.StoreID,
categoryID: req.body.CategoryID,
semiCategoryID: req.body.SemiCategoryID
}, (err, store) => { console.log(store); });

这样,您从 mongo 返回的对象必须符合前端给出的所有四个标准。

关于node.js - Mongoose如何找到特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47501445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com