如何将mongodb查询的结果保存到javascript变量中? [英] how to save a result from a mongodb query into a javascript variable?

查看:72
本文介绍了如何将mongodb查询的结果保存到javascript变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里存在一些有关如何将查询结果保存到javascript varialbe中的问题,但是我无法实现它们.关键是我有一个非常困难的查询,所以我认为这个问题是唯一的.

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 difficult query, so this question is, in my opinion, unique.

这是问题所在.我有一个名为"drives"的集合和一个名为"driveDate"的键.我需要保存1个具有最小日期的变量,以及其他具有最大日期的变量.

Here is the problem. I have a collection namend "drives" and a key named "driveDate". I need to save 1 variable with the smallest date, and other with the biggest date.

查询最小日期为:

> db.drives.find({},{"_id":0,"driveDate":1}).sort({"driveDate":1}).limit(1)

结果是:

{ "driveDate" : ISODate("2012-01-11T17:24:12.676Z") }

我如何将其保存到变量中,我可以这样做吗?

how dan I save this to a variable, can I do something like:

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

谢谢!

推荐答案

假设您正在尝试在Shell中执行此操作:

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返回一个游标,您需要对其进行迭代以检索实际文档.在光标上调用toArray会将其转换为文档数组.

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.

这篇关于如何将mongodb查询的结果保存到javascript变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆