Is it possible to get single result in aggregate?

后端 未结 4 1407
花落未央
花落未央 2020-12-18 18:08

Is it possible to get a single document on db.collection.aggregate like as in db.collection.findOne?

4条回答
  •  Happy的楠姐
    2020-12-18 18:14

    If "by a single result" you mean a findOne style return type, it should be impossible to do so. While not saying that it is impossible, it should be and should be treated as such. Aggregate operations are documented to return aggregate cursors only, so the only robust conclusion to an aggregate operation should be either an aggregate cursor or an error. When you receive your aggregate cursor, you can then use its exposed methods to access your single document. To expect anything else is akin to courting chaos.

    Ps: Arrived massively late to this thread. Hope to help others who might end up here later on.

提交回复
热议问题