Exemple #1
0
module.exports.setOptions = function (req, res, next) {
    'use strict';

    var query = operationsModel.getFindQuery();
    query
        .setOptions({
            limit: 2,
            skip: 1,
            sort: {updated_at: -1}
        })
        .where({str: /from/ig});


    query.execAsync()
        //from this point use blubird methods
        .then(function (resultsArr) {
            console.log('Results: \n' + JSON.stringify(resultsArr, null, 2));
            res.send('Results: <pre>' + JSON.stringify(resultsArr, null, 2) + '</pre>');
        })
        .catch(function (err) {
            err.status = err.status || 500;
            errorsLib.onErrorCatch(err, res);
        });

    console.log(JSON.stringify(query.getQuery(), null, 2));
    /*

     */

};
Exemple #2
0
module.exports.merge = function (req, res, next) {
    'use strict';

    var query = operationsModel.getFindQuery();

    var query2 = query.where({str: /some/ig}); //will be merged into query results

    query
        .where({str: /2/ig})
        // .where({str: /some/ig}); //will not work on same path, so use merge() to apply on same path 'str'
        .merge(query2);


    query.execAsync()
        //from this point use blubird methods
        .then(function (resultsArr) {
            console.log('Results: \n' + JSON.stringify(resultsArr, null, 2));
            res.send('Results: <pre>' + JSON.stringify(resultsArr, null, 2) + '</pre>');
        })
        .catch(function (err) {
            err.status = err.status || 500;
            errorsLib.onErrorCatch(err, res);
        });

    console.log(JSON.stringify(query.getQuery(), null, 2));
    /*
    {
        "str": {}
    }
     */

};
Exemple #3
0
module.exports.gtlt = function (req, res, next) {
    'use strict';

    //define query
    var query = operationsModel.getFindQuery();
    query
        .where('num').gt(30).lt(34);


    console.log(JSON.stringify(query.getQuery(), null, 2));
    /*
    {
      "num": {
        "$gt": 30,
        "$lt": 34
      }
    }
     */


    query.execAsync()
        .then(function (resultsArr) {
            console.log('find() results: \n' + JSON.stringify(resultsArr, null, 2));
            res.send('find() results: <pre>' + JSON.stringify(resultsArr, null, 2) + '</pre>');
        })
        .catch(function (err) {
            err.status = err.status || 500;
            errorsLib.onErrorCatch(err, res);
        });

};
Exemple #4
0
module.exports.comment = function (req, res, next) {
    'use strict';

    var query = operationsModel.getFindQuery();
    query
        .where({})
        .comment('Select doc where num=33') //just a comment
        .where({num: 33});


    query.execAsync()
        //from this point use blubird methods
        .then(function (resultsArr) {
            console.log('Results: \n' + JSON.stringify(resultsArr, null, 2));
            res.send('Results: <pre>' + JSON.stringify(resultsArr, null, 2) + '</pre>');
        })
        .catch(function (err) {
            err.status = err.status || 500;
            errorsLib.onErrorCatch(err, res);
        });
};
Exemple #5
0
module.exports.dollarwhere = function (req, res, next) {
    'use strict';

    var query = operationsModel.getFindQuery();
    query.$where(function () { //here cannot be used arrow function because mongodb doesn't accept it
        return this.str === 'From array 2 !'; //this.str is 'operations#str' field in mongoDB
        // return false; //empty array [] will be returned. Usefull to break output under certain conditions
        // throw new Error('My custom error!'); //throws error to catch()
    });


    query.execAsync()
        //from this point use blubird methods
        .tap(function (resultsArr) { //also you can use then() instead of tap()
            console.log('Results: \n' + JSON.stringify(resultsArr, null, 2));
            res.send('Results: <pre>' + JSON.stringify(resultsArr, null, 2) + '</pre>');
        })
        .catch(function (err) {
            err.status = err.status || 500;
            errorsLib.onErrorCatch(err, res);
        });
};
Exemple #6
0
module.exports.andor = function (req, res, next) {
    'use strict';

    //define query
    var query = operationsModel.getFindQuery();
    query
        .and([{str: {$regex: /som/ig}}, {num: {$lt: 35}}]);


    console.log(JSON.stringify(query.getQuery(), null, 2));
    /*
    {
      "$and": [
        {
          "str": {
            "$regex": {}
          }
        },
        {
          "num": {
            "$lt": 35
          }
        }
      ]
    }
     */


    query.execAsync()
        .then(function (resultsArr) {
            console.log('Results: \n' + JSON.stringify(resultsArr, null, 2));
            res.send('Results: <pre>' + JSON.stringify(resultsArr, null, 2) + '</pre>');
        })
        .catch(function (err) {
            err.status = err.status || 500;
            errorsLib.onErrorCatch(err, res);
        });

};
Exemple #7
0
module.exports.cursorStream = function (req, res, next) {
    'use strict';

    var query = operationsModel.getFindQuery();

    var docs = [];
    let i = 1;
    query
        .where({str: /From/ig})
        .cursor()
        .on('data', function (doc) { //this function is executed on each new doc
            docs.push(doc); //filling docs array
            console.log('doc' + i + JSON.stringify(doc, null, 2));
            i++;
        })
        .on('end', function () {
            res.send('Results: <pre>' + JSON.stringify(docs, null, 2) + '</pre>'); //'docs' is array which contains all docs
            console.log('FINISHED !!!');
        });

    console.log(JSON.stringify(query.getQuery(), null, 2));

};
Exemple #8
0
module.exports.exists = function (req, res, next) {
    'use strict';

    var query = operationsModel.getFindQuery();
    query
        .exists('mix.name');
        // .exists('mix');
        // .exists('mix.name2'); //returns empty array because name2 doesn't exist


    query.execAsync()
        //from this point use blubird methods
        .then(function (resultsArr) {
            console.log('Results: \n' + JSON.stringify(resultsArr, null, 2));
            res.send('Results: <pre>' + JSON.stringify(resultsArr, null, 2) + '</pre>');
        })
        .catch(function (err) {
            err.status = err.status || 500;
            errorsLib.onErrorCatch(err, res);
        });

    console.log(JSON.stringify(query.getQuery(), null, 2));

};
Exemple #9
0
/**
 * GET /examples/mongoose/40queryoper...
 */

require('rootpath')();
var operationsModel = require('server/app/models/examples/operations');
var errorsLib = require('server/app/lib/errorsLib');

//mongoose promisification
var Bpromise = require('bluebird');
Bpromise.promisifyAll(require('mongoose')); //enables execAsync()


var query = operationsModel.getFindQuery();


/*****************************************************************************************
* GET /examples/mongoose/40queryoper-remove *
*****************************************************************************************
* - remove([criteria], [callback]) removing all docs which matches query criteria. */
module.exports.remove = function (req, res, next) {
    'use strict';

    //first example
    // query
        // .size('obj.arr_str', 0)
        // .remove();

    //second example
    query
        .remove({'obj.arr_str': {$elemMatch: {$eq: 'pero'}}});