Sunday, 23 April 2017

Parsing a long query string directly to MongoDB (just like you can in SQL)

In PHP or C++ or Java etc. I can send a whole long complex query to MySql

myQuery = 'SELECT * FROM BOOKINGS WHERE STATUS < 5 ORDER BY CHECKIN';
resultArray = mySql_query(myConnnect,myQuery);

Now I am working with MEAN using MongoDB. Is there a way to do the same here?

var express = require('express');
var app = express();
app.get(myDatabase, function(req, res){

    my-long-query = 'db.getCollection("receipt").aggregate([{$match:{bookingId:"94JCI"}},{$project:{"_id":0,"receiptNo":1,"amount":1}}])'
    db.myDatabase.parse-my-long-query(function(err, docs) {
        res.json(docs);
    });

});

Some of the queries are really long and I generate them dynamically Right now I copy paste them to Robomongo test the results.

Any ideas?



via torbenrudgaard

No comments:

Post a Comment