added workaround for 'added' field compatible to MongoDB 3.6
This commit is contained in:
		@@ -256,7 +256,7 @@ router.get('/samples', async (req, res, next) => {
 | 
			
		||||
    // projection.added = {$toDate: '$_id'};
 | 
			
		||||
    // projection.added = { $convert: { input: '$_id', to: "date" } }  // TODO: upgrade MongoDB version or find alternative
 | 
			
		||||
  }
 | 
			
		||||
  if (!(filters.fields.indexOf('_id') >= 0)) {  // disable _id explicitly
 | 
			
		||||
  if (filters.fields.indexOf('_id') < 0 && filters.fields.indexOf('added') < 0) {  // disable _id explicitly
 | 
			
		||||
    projection._id = false;
 | 
			
		||||
  }
 | 
			
		||||
  queryPtr.push({$project: projection});
 | 
			
		||||
@@ -266,8 +266,18 @@ router.get('/samples', async (req, res, next) => {
 | 
			
		||||
      if (err) return next(err);
 | 
			
		||||
      if (data[0].count) {
 | 
			
		||||
        res.header('x-total-items', data[0].count.length > 0 ? data[0].count[0].count : 0);
 | 
			
		||||
        res.header('Access-Control-Expose-Headers', 'x-total-items');
 | 
			
		||||
        data = data[0].samples;
 | 
			
		||||
      }
 | 
			
		||||
      if (filters.fields.indexOf('added') >= 0) {  // add added date
 | 
			
		||||
        data.map(e => {
 | 
			
		||||
          e.added = e._id.getTimestamp();
 | 
			
		||||
          if (filters.fields.indexOf('_id') < 0) {
 | 
			
		||||
            delete e._id;
 | 
			
		||||
          }
 | 
			
		||||
          return e
 | 
			
		||||
        });
 | 
			
		||||
      }
 | 
			
		||||
      if (filters['to-page'] < 0) {
 | 
			
		||||
        data.reverse();
 | 
			
		||||
      }
 | 
			
		||||
@@ -289,7 +299,15 @@ router.get('/samples', async (req, res, next) => {
 | 
			
		||||
    res.write('[');
 | 
			
		||||
    let count = 0;
 | 
			
		||||
    const stream = collection.aggregate(query).cursor().exec();
 | 
			
		||||
    stream.on('data', data => { res.write((count === 0 ? '' : ',\n') + JSON.stringify(data)); count ++; });
 | 
			
		||||
    stream.on('data', data => {
 | 
			
		||||
      if (filters.fields.indexOf('added') >= 0) {  // add added date
 | 
			
		||||
        data.added = data._id.getTimestamp();
 | 
			
		||||
        if (filters.fields.indexOf('_id') < 0) {
 | 
			
		||||
          delete data._id;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      res.write((count === 0 ? '' : ',\n') + JSON.stringify(data)); count ++;
 | 
			
		||||
    });
 | 
			
		||||
    stream.on('close', () => {
 | 
			
		||||
      res.write(']');
 | 
			
		||||
      res.end();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user