如何解决过滤一对多MongoDB
猫鼬一对多,填充后过滤结果 我需要找到所有购物车项目,按ID过滤后,如果购物车中存在产品ID,则该做什么或不存在。我在Google中进行搜索,但没有找到针对我的具体解决方案。
摘要示例 非常感谢你。
const order = OrderNew.find().all
-> Result
{
"order_id" : 1995,"ordered" : false,carts: [
{id:1,name:"Nike"},{id:2,name:"Diadora"},{id:3,name:"Lacoste"}
]
}
order.filter(carts.id == 1)
print(order.length)
// printed 1
order.filter(carts.id == 4)
print(order.length)
// printed 0
我的路线功能 路线/products.js
order_cart_qs = await OrderNew.find()
.populate({
path: 'carts',match: { product:{_id:order_item.product._id}}
})
console.log(order_cart_qs.length);
我的订单模型 型号/order_new.js
const mongoose = require('mongoose');
const orderNewSchema = mongoose.Schema({
_id:mongoose.Schema.Types.ObjectId,ordered: {
type: Boolean,required:true
},carts: [
{
type: mongoose.Schema.Types.ObjectId,ref: "CartItem"
}
],finish: {
type: Boolean,author: {
type: mongoose.Schema.Types.ObjectId,ref: 'User',required: true
},});
module.exports = mongoose.model('OrderNew',orderNewSchema);
我的购物车型号 型号/ cart_item.js
const mongoose = require('mongoose');
const cartItemSchema = mongoose.Schema({
_id:mongoose.Schema.Types.ObjectId,product: {
type: mongoose.Schema.Types.ObjectId,ref: 'Product',quantity: {
type: Number,default: 1
},});
module.exports = mongoose.model('CartItem',cartItemSchema);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。