how to save a result from a mongodb query into a javascript variable?

后端 未结 2 1019
广开言路
广开言路 2021-01-14 00:33

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 mu

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 01:27

    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.

提交回复
热议问题