Mongodb:购物车什么时候到期?
我正在通过ExpressJs+Mongodb建立一个电子商务网站,我一直在关注这个问题: 什么时候我们需要在技术上终止购物车(移除购物车并将产品返回库存)?用户何时访问购物车?还是我需要一份cron工作 我关注了这篇文章: 以下是我的购物车模型的实现:Mongodb:购物车什么时候到期?,mongodb,express,shopping-cart,Mongodb,Express,Shopping Cart,我正在通过ExpressJs+Mongodb建立一个电子商务网站,我一直在关注这个问题: 什么时候我们需要在技术上终止购物车(移除购物车并将产品返回库存)?用户何时访问购物车?还是我需要一份cron工作 我关注了这篇文章: 以下是我的购物车模型的实现: 'use strict'; const mongoose = require('mongoose'); const Schema = mongoose.Schema; const CartItem = new Schema({ prod
'use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CartItem = new Schema({
product: { type: Schema.Types.ObjectId, ref: 'Product' },
quantity: Number
});
const Cart = new Schema({
userSessionId: String,
status: {
type: String,
enum: [ 'active', 'completed', 'expiring', 'expired' ],
default: 'active'
},
items: [ CartItem ],
modifiedOn: { type: Date }
});
Cart.static({
summary: function(params, cb) {
this.aggregate([
{
$match: { userSessionId: params.userSessionId }
},
{
$unwind: {
path: '$items'
}
},
{
$lookup: {
from: 'products',
localField: 'items.product',
foreignField: '_id',
as: 'product'
}
},
{
$unwind: {
path: '$product',
preserveNullAndEmptyArrays: true
}
},
{
$group: {
_id: { userSessionId: '$userSessionId' },
count: { $sum: '$items.quantity' },
total: { $sum: { $multiply: [ '$product.price', '$items.quantity' ] } }
}
}
], (err, results) => cb(err, results[0]));
},
addProduct: function(params, cb, test) {
var d = new Date();
if (test) {
d.setMinutes(d.getMinutes() - 10);
}
this.findOneAndUpdate(
{ userSessionId: params.userSessionId },
{ $set: { modifiedOn: d } },
{ upsert: true, new: true }, (err, cart) => {
if (err) {
return cb(err);
}
const index = cart.items.findIndex((item) => {
return item.product.equals(params.productId);
});
if (index === -1) {
cart.items.push({
product: params.productId,
quantity: params.quantity
});
} else {
cart.items[index].quantity += parseFloat(params.quantity);
}
cart.save(cb);
});
},
updateQuantity: function(params, cb) {
this.findOneAndUpdate(
{ userSessionId: params.userSessionId },
{},
{ upsert: true, new: true }, (err, cart) => {
if (err) {
return cb(err);
}
const index = cart.items.findIndex((item) => {
return item.product.equals(params.productId);
});
if (index === -1) {
return cb(new Error('Can not find product in cart'));
}
cart.items[index].quantity = params.quantity;
cart.save(cb);
});
},
findItem: function(params, cb) {
this.findOne({ userSessionId: params.userSessionId }).exec((err, cart) => {
if (err) {
return cb(err);
}
const index = cart.items.findIndex((item) => {
return item.product.equals(params.productId);
});
if (index === -1) {
return cb(new Error('Can not find product in cart'));
}
cb(null, cart.items[index]);
});
},
removeProduct: function(params, cb) {
this.update(
{ userSessionId: params.userSessionId },
{
$pull: { items: { product: params.productId } },
$set: { modifiedOn: new Date() }
},
cb
);
},
getExpiredCarts: function(params, cb) {
var now = new Date();
if (typeof params.timeout !== 'number') {
return cb(new Error('timeout should be a number!'));
}
now.setMinutes(now.getMinutes() - params.timeout);
this.find(
{ modifiedOn: { $lte: now }, status: 'active' }
).exec(cb);
}
});
mongoose.model('Cart', Cart);
您应该使用某种分布式会话来存储购物车 我认为您正在寻找类似于:
它使用了
expressjs会话
和mongodb
,然后您就拥有了一个分布式缓存,它将与您的应用程序的多个实例一起工作。hello@marco,我实际上使用的是expressjs会话。我关心的是我们何时需要在技术上终止购物车(移除购物车并将产品返回库存)?用户何时访问购物车?还是我需要一份cron工作
如果您使用的是mongodb
,则可以设置ttl
,在一定时间后删除文档。如果这样做,就不需要使用cron作业
。您可以在X天后过期。谢谢,我会尝试您的建议