Database query recipes

Open DevTools to see the results after hitting the Run button

User random sample


xenia()
  .collection('users')
  .sample(10)
.exec()
.then(function(res){
  console.log(res.results)
})
        

User with most comments statistics


xenia()
  .include(['name', 'avatar',
            'statistics.comments.all.all',
            'stats'])
  .sort(['stats.comments', -1])
  .limit(10)
.exec()
  .then(function(res){
    console.log(res.results)
  })
        

User comments distribution


xenia()
  .project({
    count: {
      $subtract: ['$stats.comments', {
      /* 50 is the interval size.
         A higher number means
        less datapoints */
        $mod: ['$stats.comments', 50]
      }]
    }, _id: false })
  .group({ _id: '$count', count: { $sum: 1 } })
  .sort(['_id', 1])
.exec()
  .then(function(res){
    console.log(res.results[0].Docs)
  })
        

Users with higher flagged comments ratio


var flagged = 'statistics.comments'
flagged += '.all.ratios.SystemFlagged'

xenia()
  .include(['name', 'avatar', flagged])
  .sort([flagged, -1])
  .limit(20)
.exec()
  .then(function(res) {
    console.log(res.results[0].Docs)
  })
        

Oldest unmoderated comments


xenia()
  .collection('comments')
  .include(['body', 'date_created', 'status'])
  .match({ status: 'Untouched' })
  .sort(['date_created', 1])
  .limit(20)
.exec()
  .then(function(res) {
    console.log(res.results[0].Docs)
  })
        

Comments sample with associated article info


xenia()
  .collection('comments')
  .sample(20)
  .join('assets', '_id', 'asset_id', 'article')
.exec()
  .then(function(res) {
    console.log(res.results[0].Docs)
  })
        

Paginated comments


function getComments(page, pageSize) {
  return xenia()
    .collection('comments')
    .limit(pageSize)
    // page count starts in 1
    .skip(pageSize * (page - 1))
  .exec()
}

getComments(3, 15)
.then(function(res) {
  console.log(res.results[0].Docs)
});