MongoDB聚合与未来文档

4

我希望能够得到一些关于如何在MongoDB中最好地处理这种类型查询的指导。

我有一个购买数据库,每个购买都有一个course属性以及一个购买日期。

我想要的是在某人购买初始产品后发生的购买清单。

因此 - 这里是一些伪查询:

// first find everyone who signed up for course A
{ course: 'a' }

那么

/*
  out of those people, filter for those who in the future signed up
  for another course
*/

{
  course: { $in: ['b','c','d']},
  date: { $gt: $courseA.purchaseDate }
}

这是否可以使用聚合来实现?还是每次初始购买都需要进行多个数据库调用以检查是否有未来的购买?

以下是每次购买的数据示例:

{ email: 'wes@example.com', course: 'a', purchaseDate: 10 },
{ email: 'wes@example.com', course: 'b', purchaseDate: 20 }, // match
{ email: 'wes@example.com', course: 'c', purchaseDate: 5 }, // not
{ email: 'nancy@example.com', course: 'a', purchaseDate: 5 },
{ email: 'nancy@example.com', course: 'c', purchaseDate: 6 }, // match
{ email: 'nancy@example.com', course: 'b', purchaseDate: 10 }, // match
{ email: 'nancy@example.com', course: 'd', purchaseDate: 1 }, // not

你能发布一个样例文档吗? - joao
刚刚发布了一个示例-谢谢。 - wesbos
不要像你描述的那样去解决问题,我建议做以下两件事:
  1. 把所有购买了课程的用户分组,每个用户只保留一个文档,包含他们所购买的所有课程。
  2. 过滤掉未购买课程x的所有用户。
这样做不会给你所有在某门课程之后购买的课程,但可以让你更接近目标。
- rrag
2个回答

2

在Twitter上得到了一些帮助,找到了答案

.aggregate([
  // Project the target purchasedate
  { $match: { course: { $in: ['JS3', 'RDX', 'WTF', 'RFB', 'ES6']}}},
  { $project: {
    email: 1, amount: 1, course: 1, purchaseDate: 1,
    target: {
      $cond: {
        if: { $eq: ['$course', 'JS3'] },
        then: "$purchaseDate",
        else: 0,
      }
    }
  }},
  // Group records by email, storing the target and the highest encountered purchase date
  { $group: {
    _id: '$email',
    courses: { $push: '$course'},
    amounts: { $push: '$amount'},
    count: { $sum: 1 },
    spent: { $sum: '$amount' },
    target: { $first: '$target'},
    max: { $max: '$purchaseDate'}
  }},
  // // Check if the highest encountered date is greater then the target
  { $project: {
    email: 1, course: 1, amounts: 1, spent: 1, count: 1, courses: 1, target:1, max: 1,
    selected: { $cond: [{ $gt: ['$max', '$target']}, true, false] }
  }},
  // Filter out the non-matches
  { $match: {
    target: { $gt: 0 },
    selected: true,
    spent: { $gt: 0 },
  }},
  { $sort: { spent: -1 }}
])

0
我建议做一个类似于MapReduce的东西:
const options = {
  query: { course: 'courseA' }, // filter only the A course purchased
  map: function(){
    // iterate over course to get the couseA timeStamp

    // for every course you may have here, compare to couseATime
    this.course.forEach(function(course){
       // extract all the timeStamps
    })

    // if courseTime > couseATime then emit
    // emit({_id:this._id, email:this.email}, 1)
  },
  reduce: function(key, val){ // key will be {_id:'', email:''}
    return val // not important
  }
}

Model
  .mapReduce(options)
  .then(function(docs){
  })

你应该能够获取到购买了非courseA课程的人的所有_id和email,且购买日期在指定日期之后。

另一种方法是使用.aggregate()、.project()和.match(),但是在不同的管道步骤中获取courseATimeStamp可能会有问题。

编辑:
我假设course是一个数组...如果course字段是一个字符串,那么这个解决方案也可以适用。

...
// Emit every course with the same key (_id + email)
map(){
  emit({_id: this._id, email:this.email}, {course: this.course, date: this.couse.date})
},

reduce(key, values){
  // key is {_id:'', email:''} and is unique
  // values is an array of {course:'', date:''}
  var aDate
  var dates = []

  // Find other dates
  values.forEach(function(val){
    // isolate course A date => aDate = ...
    // populate dates[] with date + course name
  })

  // dates should be => [{courseB: 10, couseC: 15 ...}]
  // The mean used to saved the couse + date is up to you

  // filter dates $gt aDate
  dates = dates.filter(....)

  return dates
}

注意:因为只有在发出超过一次时才调用reduce(),所以可能会得到奇怪的结果:

  • 如果您只发出一个值,则结果值将是您发出的值
  • 如果您发出多个值,则结果值将是reduce()中过滤后的数组

(如果您发出一个数字,并且您的reduce也是一个数字——比如项目的总数,这就有意义了)

因此,如果您需要清理结果,您可能希望使用mongoose提供的finalize()。


this.course 是什么?它如何找到其他课程? - wesbos
在 map 函数中,this 表示当前文档。因此,this.course 是“course”字段,如果这是一个数组,你可以进行映射。 - Benjamin Mosnier

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接