Mongoose`$project`如何避免重复文档
我在Mongoose`$project`如何避免重复文档,mongoose,aggregation-framework,Mongoose,Aggregation Framework,我在$projection中遇到了问题。实际上,我有两个独立的集合AddToCart和Product集合向购物车添加产品这是一个对象数组。如果add\u to\u cart\u products长度为1则检索正确的格式。如果大于1,则表示打印两次 AddToCart系列 [ { “向购物车用户添加”:“5f0076b7bd530928fc0c0285”, “向购物车添加商品”:[ { “产品”:“5f05a0270b4f3a5c41c70826”, “产品_项”:“5f05a0270b4f3a5
$projection
中遇到了问题。实际上,我有两个独立的集合AddToCart
和Product
集合<代码>向购物车添加产品这是一个对象数组。如果add\u to\u cart\u products
长度为1
则检索正确的格式。如果大于1,则表示打印两次
AddToCart系列
[
{
“向购物车用户添加”:“5f0076b7bd530928fc0c0285”,
“向购物车添加商品”:[
{
“产品”:“5f05a0270b4f3a5c41c70826”,
“产品_项”:“5f05a0270b4f3a5c41c70877”,
“产品数量”:5
},
{
“产品”:“5f05a0270b4f3a5c41c70827”,
“产品项”:“5f05a0270b4f3a5c41c70666”,
“产品数量”:3
}
],
“添加到购物车产品总数”:5,
“向购物车添加折扣”:50,
“向税中添加”:“5EAE321D2192480012F978E”,
“添加到购物车总计”:500
}
]
产品数据库:
lookups: [
{
from: 'shop_db_products',
let: {
productId: '$add_to_cart_products.product',
purchaseQuantity: '$add_to_cart_products.product_quantity',
productItemId: '$add_to_cart_products.product_item',
},
pipeline: [
{
$match: { $expr: { $in: ['$_id', '$$productId'] } },
},
{
$lookup: {
from: 'shop_db_products',
localField: 'product_id',
foreignField: '_id',
as: 'products',
},
},
{
$project: {
_id: true,
product: {
_id: '$_id',
product_name: '$product_name',
},
product_purchase_quantity: '$$purchaseQuantity',
product_item: {
$reduce: {
input: {
$filter: {
input: '$product_items',
cond: {
$in: ['$$this._id', '$$productItemId'],
},
},
},
initialValue: {},
in: {
_id: '$$this._id',
product_size: { $concat: [{ $toString: '$$this.product_size.value' }, '$$this.product_size.unit'] },
product_price: '$$this.product_price',
product_type: '$$this.product_type'
},
},
},
},
},
{
$unwind: '$product_purchase_quantity',
},
],
localField: '',
as: 'add_to_cart_products',
model: 'ProductModel',
},
],
[
{
“_id”:“5f05a0270b4f3a5c41c70826”,
“产品名称”:“2Gingers”,
“产品项目”:[
{
“_id”:“5f05a0270b4f3a5c41c70877”,
“产品项目编号”:“857566003019”,
“产品价格”:20.99,
“产品尺寸”:“750ml”,
“产品类型”:“瓶子”
}
]
},
{
“_id”:“5f05a0270b4f3a5c41c70827”,
“产品名称”:“Alverty”,
“产品项目”:[
{
“_id”:“5f05a0270b4f3a5c41c70666”,
“产品项目编号”:“857566003019”,
“产品价格”:20.99,
“产品尺寸”:“750ml”,
“产品类型”:“瓶子”
}
]
}
]
添加到查找和投影:
lookups: [
{
from: 'shop_db_products',
let: {
productId: '$add_to_cart_products.product',
purchaseQuantity: '$add_to_cart_products.product_quantity',
productItemId: '$add_to_cart_products.product_item',
},
pipeline: [
{
$match: { $expr: { $in: ['$_id', '$$productId'] } },
},
{
$lookup: {
from: 'shop_db_products',
localField: 'product_id',
foreignField: '_id',
as: 'products',
},
},
{
$project: {
_id: true,
product: {
_id: '$_id',
product_name: '$product_name',
},
product_purchase_quantity: '$$purchaseQuantity',
product_item: {
$reduce: {
input: {
$filter: {
input: '$product_items',
cond: {
$in: ['$$this._id', '$$productItemId'],
},
},
},
initialValue: {},
in: {
_id: '$$this._id',
product_size: { $concat: [{ $toString: '$$this.product_size.value' }, '$$this.product_size.unit'] },
product_price: '$$this.product_price',
product_type: '$$this.product_type'
},
},
},
},
},
{
$unwind: '$product_purchase_quantity',
},
],
localField: '',
as: 'add_to_cart_products',
model: 'ProductModel',
},
],
当前响应:
lookups: [
{
from: 'shop_db_products',
let: {
productId: '$add_to_cart_products.product',
purchaseQuantity: '$add_to_cart_products.product_quantity',
productItemId: '$add_to_cart_products.product_item',
},
pipeline: [
{
$match: { $expr: { $in: ['$_id', '$$productId'] } },
},
{
$lookup: {
from: 'shop_db_products',
localField: 'product_id',
foreignField: '_id',
as: 'products',
},
},
{
$project: {
_id: true,
product: {
_id: '$_id',
product_name: '$product_name',
},
product_purchase_quantity: '$$purchaseQuantity',
product_item: {
$reduce: {
input: {
$filter: {
input: '$product_items',
cond: {
$in: ['$$this._id', '$$productItemId'],
},
},
},
initialValue: {},
in: {
_id: '$$this._id',
product_size: { $concat: [{ $toString: '$$this.product_size.value' }, '$$this.product_size.unit'] },
product_price: '$$this.product_price',
product_type: '$$this.product_type'
},
},
},
},
},
{
$unwind: '$product_purchase_quantity',
},
],
localField: '',
as: 'add_to_cart_products',
model: 'ProductModel',
},
],
{
“_id”:“5fa257b3eb6a3f6e3e02ff3f”,
“将\添加到\购物车\状态\处于\活动状态”:true,
“向购物车添加折扣”:50,
“向购物车加税”:8,
“向购物车添加商品”:[
{
“_id”:“5f059f8e0b4f3a5c41c6f54c”,
“产品”:{
“_id”:“5f05a0270b4f3a5c41c70826”,
“产品名称”:“2Gingers”
},
“产品采购数量”:5,
“产品项”:{
“_id”:“5f05a0270b4f3a5c41c70877”,
“产品项目编号”:“857566003019”,
“产品价格”:20.99,
“产品尺寸”:“750ml”,
“产品类型”:“瓶子”
}
},
{
“_id”:“5f059f8e0b4f3a5c41c6f54c”,
“产品”:{
“_id”:“5f05a0270b4f3a5c41c70826”,
“产品名称”:“2Gingers”
},
“产品采购数量”:5,
“产品项”:{
“_id”:“5f05a0270b4f3a5c41c70877”,
“产品项目编号”:“857566003019”,
“产品价格”:20.99,
“产品尺寸”:“750ml”,
“产品类型”:“瓶子”
}
},
{
“_id”:“5f059f8e0b4f3a5c41c6f54c”,
“产品”:{
“_id”:“5f05a0270b4f3a5c41c70827”,
“产品名称”:“Alverty”
},
“产品采购数量”:3,
“产品项”:{
“_id”:“5f05a0270b4f3a5c41c70666”,
“产品项目编号”:“857566003019”,
“产品价格”:20.99,
“产品尺寸”:“750ml”,
“产品类型”:“瓶子”
}
},
{
“_id”:“5f059f8e0b4f3a5c41c6f54c”,
“产品”:{
“_id”:“5f05a0270b4f3a5c41c70827”,
“产品名称”:“Alverty”
},
“产品采购数量”:3,
“产品项”:{
“_id”:“5f05a0270b4f3a5c41c70666”,
“产品项目编号”:“857566003019”,
“产品价格”:20.99,
“产品尺寸”:“750ml”,
“产品类型”:“瓶子”
}
}
],
“添加到购物车创建日期”:“2020-11-04T07:26:43.885Z”,
“添加到购物车修改日期”:“2020-11-04T07:26:43.885Z”,
“添加到购物车编号”:“ATC00003”,
“将\添加到\购物车\用户”:{
“用户全名”:“供应商”,
“用户电子邮件”:vendor@techardors.com",
“用户电话”:“0123456785”
}
}
预期响应:
lookups: [
{
from: 'shop_db_products',
let: {
productId: '$add_to_cart_products.product',
purchaseQuantity: '$add_to_cart_products.product_quantity',
productItemId: '$add_to_cart_products.product_item',
},
pipeline: [
{
$match: { $expr: { $in: ['$_id', '$$productId'] } },
},
{
$lookup: {
from: 'shop_db_products',
localField: 'product_id',
foreignField: '_id',
as: 'products',
},
},
{
$project: {
_id: true,
product: {
_id: '$_id',
product_name: '$product_name',
},
product_purchase_quantity: '$$purchaseQuantity',
product_item: {
$reduce: {
input: {
$filter: {
input: '$product_items',
cond: {
$in: ['$$this._id', '$$productItemId'],
},
},
},
initialValue: {},
in: {
_id: '$$this._id',
product_size: { $concat: [{ $toString: '$$this.product_size.value' }, '$$this.product_size.unit'] },
product_price: '$$this.product_price',
product_type: '$$this.product_type'
},
},
},
},
},
{
$unwind: '$product_purchase_quantity',
},
],
localField: '',
as: 'add_to_cart_products',
model: 'ProductModel',
},
],
{
“_id”:“5fa257b3eb6a3f6e3e02ff3f”,
“将\添加到\购物车\状态\处于\活动状态”:true,
“向购物车添加折扣”:50,
“向购物车加税”:8,
“向购物车添加商品”:[
{
“_id”:“5f059f8e0b4f3a5c41c6f54c”,
“产品”:{
“_id”:“5f05a0270b4f3a5c41c70826”,
“产品名称”:“2Gingers”
},
“产品采购数量”:5,
“产品项”:{
“_id”:“5f05a0270b4f3a5c41c70877”,
“产品项目编号”:“857566003019”,
“产品价格”:20.99,
“产品尺寸”:“750ml”,
“产品类型”:“瓶子”
}
},
{
“_id”:“5f059f8e0b4f3a5c41c6f54c”,
“产品”:{
“_id”:“5f05a0270b4f3a5c41c70827”,
“产品名称”:“Alverty”
},
“产品采购数量”:3,
“产品项”:{
“_id”:“5f05a0270b4f3a5c41c70666”,
“产品项目编号”:“857566003019”,
“产品价格”:20.99,
“产品尺寸”:“750ml”,
“产品类型”:“瓶子”
}
}
],
“添加到购物车创建日期”:“2020-11-04T07:26:43.885Z”,
“添加到购物车修改日期”:“2020-11-04T07:26:43.885Z”,
“添加到购物车编号”:“ATC00003”,
“将\添加到\购物车\用户”:{
“用户已满”