javascript - I am trying to query mongoDB by having the user enter a name in a search box, then finding all documents that match that search -
the code have far allows me query database finding specific key:value in documents, want able pass argument function user's search becomes 'value' in key:value query. currently, have "cosmic black" in place of want user's argument be.
var findmaterials = function(db, callback) { var cursor = db.collection('materials').find( {"material_name": "cosmic black"} ); cursor.each(function(err, doc) { assert.equal(err, null); if (doc !== null) { console.log(doc); } else { callback(); } }); };
i don't know how argument in there, or if it's possible current code setup. thoughts appreciated.
var findmaterials = function(db, value, callback) { var cursor = db.collection('materials').find( {"material_name": value} ); cursor.each(function(err, doc) { assert.equal(err, null); if (doc !== null) { console.log(doc); } else { callback(); } }); };
how you're calling findmaterials(), that's different question. should trick.
function doallthework(url, value){ mongoclient.connect(url, function(err, db) { assert.equal(null, err); findmaterials(db, value, function() { db.close(); }); }); }
pass value sort of param. if you're building api pluck out of json. if you're running cmd line same via methodology.
Comments
Post a Comment