我正在尝试按销售集合中的产品进行分组,并将其总计相加,以了解哪些是我的应用程序中最畅销的产品。
MONGOOSE模型
import UIKit
class SecondViewController: UIViewController {
@IBOutlet var textField: UITextField!
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .darkGray
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
//The line below will display the keyboard over the UIView, thus obscuring it.
textField.becomeFirstResponder()
}
@objc func keyboardWillShow(_ notification: Notification) {
let info = notification.userInfo!
let kbHeight = (info[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
bottomConstraint.constant = -kbHeight
let duration: TimeInterval = (info[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
UIView.animate(withDuration: duration) { self.view.layoutIfNeeded() }
}
}
const DHCustomerinvoiceSchema = mongoose.Schema({
const mongoose = require('mongoose');
收藏示例
Saledetail: {
type: Array,
required: true
},
date:{
type: Date,
required: true
},
total:{
type: Number,
required: true
},
pay:{
type: Number,
required: true,
default: 0
},
topay:{
type: Number,
required: true
},
user:{
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'UserDH'
},
customer:{
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'DHcontacto'
},
state:{
type: String,
default: "OWED"
},
created:{
type: Date,
default: Date.now()
},
});
module.exports = mongoose.model('DHCustomerinvoice', DHCustomerinvoiceSchema);
分辨率:
{
"id": "5ef6*****",
"Saledetail": [
{
"id": "5ebf*****",
"quantity": 9,
"price": 2000,
"totalline": 18000
}
],
"datesale": "1593129600000",
"grandtotal": 18000,
"user": "5eb9ab******",
"customer": {
"name": "isabella"
},
"state": "PAID"
},
我使用了很多方法,这些方法实际上并没有给我带来这个结果,但是在找到我的最佳客户时,我取得了积极的结果,这些客户实际上是通过汇总,匹配和分组来开发它的,并且也应用了排序和限制... 但是在这个示例中,我并没有取得成功,我想这是因为采购细节的安排使馆藏的体系结构与众不同
答案 0 :(得分:0)
我的信誉不足,无法评论您的问题。因此,我将其作为答案。
我认为您可以使用$ elemMatch在数组中搜索该项目。
const Productos = await Factura.aggregate([{ detlle: { $elemMatch: { $gte: 80, $lt: 85 } } }])
有关更多详细信息,elemMatch
答案 1 :(得分:0)
在我的问题的答案下面
mostSellingProducts: async (_,{},ctx)=>{
const Products = await Invoice.aggregate([
{ $unwind: "$Saledetail" },
{ $match: { "state" : 'PAY'}},
{ $group: {
_id : { $toObjectId: "$Saledetail.id" },
total: { $sum: '$Saledetail.totalline' }
}},
{
$lookup: {
from: 'dhproducts',
localField: '_id',
foreignField: "_id",
as: "products"
}
},
{
$limit: 4
},
{
$sort : {total: -1}
}
]);
return Products;
},