node.js - How can I filter arrays in mongodb using mongoose?
问题描述
I have created database with two collections. Each of these collections connected with relations. Here I want to pass one item _id and check whether it passed to other collection as foreign key. If it's passed, I want to filter all items which consist as _id. How can I do that. Here my mongoose query and screenshot of db. Thank you
route.get("/:id",(req,res)=>{
Vehicles.find({
categories: [req.params.id]
}, (err, data)=>{
if(err){
console.log(err);
}else{
console.log(data);
}
});
PS: For an example I want to get all vehicles which have category id "60c58c2dcf82de0780051378" inside categories array.
解决方案
在 mongo document之后,您可以查询包含 objectId作为其元素之一categories
的数组的所有文档。req.params.id
Vehicles.find({
categories: req.params.id,
}, (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
推荐阅读
- python - python的密码学抛出AttributeError:'int'对象没有属性'value'
- regex - 使用 scala 进行分组提取
- android - 如何自定义 AutoCompleteTextView 下拉位置
- angular - Angular 6 我应该为管理仪表板创建单独的应用程序还是单独的模块?
- javascript - 如何在 React js 中对本地保存的 JSON 进行 CRUD 操作
- oracle - 在 oracle 18 中创建新用户时出现错误 65096。如何使用 sql-developer 在 oracle 18 中创建用户?
- c++ - 为什么当我尝试返回引用时我的对象仍然被复制
- mysql - 用于访问 MySql 云数据库的 Powershell 脚本
- git - gitflow init,里面发生了什么,执行之后
- intellij-idea - Intellij 是否具有像 Toad 这样的自动替换功能?