json - How to query mongoDB using mongoose? -


how query mongodb using mongoose node.js? have can insert json object db, ways have tried return json object db return null or information database.

is there method using mongoose able query database similar method:

var cursor = db.collection.find() var jsonobject = cursor.next() 

here's in code right now:

mongoose.connect('mongodb://localhost/mydb'); mongoose.connection.on('error', console.error.bind(console, 'connection error:')); var cursor = mongoose.connection.db.contents.find(); console.log(cursor.next()); 

this throws error @ line :

var cursor = mongoose.... 

claiming 'cannot call method 'find' of undefined'.

note, collection 'contents' in fact exist, , contains 1 json document. know because manually navigated collection using mongo shell.

edit: open alternative methods querying database. want able return json objects db 1 @ time, while keeping track of client @ in database.

one method query mongodb using mongoose follows:

content.findone().exec(function(err,docs){console.log(docs)}); 

docs contains json object. access attributes other object.

note, method uses asynchronous call backs. such, can't store json object docs variable in order use json document information outside of function. therefore, need perform whatever actions needed json docs object information inside of function.

for example, needing query information provide filepath api. such, result follows:

//get app.get('/api/media/', function(req,res){     content.findone().exec(function(err,docs){res.sendfile(path.join(__dirname, '/api/media/', docs.filename))});  }); 

note, content model of schema, , 1 of parameters filename.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -