Reputation: 51
Inside of the command line I can display a Mongodb collection with db.collection.find(), but I can't find the equivalent to that for a get request in Node. Here's my Express route:
app.get('/latest/imagesearch', (req, res) => {
MongoClient.connect(mLab, function(err, db) {
if (err) {
console.log("Unable to connect to server", err);
} else {
console.log("Connected to server");
var collection = db.collection('links');
// res.send(collection.find());
db.close();
}
});
});
And the database info:
{ "_id" : ObjectId("58cf0485a3c6700a3eb8b373"), "term" : "dogs", "when" : 1489962117592 }
Upvotes: 0
Views: 1284
Reputation: 5928
After creating the collection object, you can call collection.find:
app.get('/latest/imagesearch', (req, res) => {
MongoClient.connect(mLab, function(err, db) {
if (err) {
console.log("Unable to connect to server", err);
return res.send("Unable to connect to server");
} else {
console.log("Connected to server");
var collection = db.collection('links');
collection.find().toArray(function(err, docs) {
return res.json(docs);
});
}
});
});
Upvotes: 1