Reputation: 55
I am trying to use node.js to provide json results from MongoDB.
var http = require('http');
var mongo = require('mongoskin');
http.createServer(function (req, res) {
var args = req.url.split("/");
console.log(args);
var searchCollection = args[1];
var searchVar = args[2];
var searchString = args[3];
var conn = mongo.db('user:[email protected]:10039/name',{safe:true});
conn.collection(searchCollection).find({searchVar:searchString}).toArray(function(err, items){
if(err) throw err;
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(items));
});
}).listen(process.env.PORT, process.env.IP);
The problem I am having is when I call the find function on the database it:-
Any help would be appreciated. Thanks!
Upvotes: 2
Views: 71
Reputation: 38509
You will need to create your query object something like this:
var query = {};
query[searchVar] = searchString;
And then pass this into your query:
conn.collection(searchCollection).find(query).toArray(function(err, items){
if(err) throw err;
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(items));
});
Upvotes: 1