Skip to content Skip to sidebar Skip to footer

How To Save A Result From A Mongodb Query Into A Javascript Variable?

there are some questions here regarding how to save a result from a query into a javascript varialbe, but I'm just not able to implement them. The point is that I have a much diffi

Solution 1:

Assuming you're trying to do this in the shell:

 tmp = db.drives.find({}, {_id:0, driveDate:1}).sort({driveDate:1}).limit(1).toArray()[0]

find returns a cursor that you need to iterate over to retrieve the actual documents. Calling toArray on the cursor converts it to an array of docs.


Solution 2:

After some time figuring out, I got the solution. here it is, for future reference:

var cursor = db.drives.find({},{"_id":1}).sort({"driveDate":1}).limit(1)

Then I can get the document from the cursor like this

var myDate = cursor.next()

That's it. Thanks for your help


Post a Comment for "How To Save A Result From A Mongodb Query Into A Javascript Variable?"