为什么不能从异步聚合请求中获得响应?

时间:2019-04-19 08:17:03

标签: mongodb

我想使用mongodb查询的结果。

这很好:

(async () =>{
    let client;
    try {
        client = await MongoClient;
        let collection = client.db("booliscraper").collection("sold");


        let docs = await collection.find(/*ObjectId("5beca41c78f21248ab47f48f")*/);
        await docs.forEach((sale) => {

            // do stuff
        });
    client.close(); 

  } catch(err) {
    console.log(err)
  }
})()

但是当我尝试做一些类似的事情时:

(async () =>{
    let client;
    try {
        client = await MongoClient;
        let collection = client.db("booliscraper").collection("sold");
        let avgPrice = await collection.aggregate([
            {$match: {myArea: {$exists: true}} },
            {$group: {_id: {area: "$myArea", year: { $year: "$soldDate" }}, "sqmPrice": {$avg: "$sqmPrice"} }}
        ])
        await console.log(avgPrice);

    client.close(); 

  } catch(err) {
    log(err)
  }
})()

输出为AggregationCursor,会话也不会关闭

0 个答案:

没有答案