猫鼬-获取_id列表,而不是具有_id的对象数组 [英] Mongoose - Get list of _ids instead of array of objects with _id

查看:117
本文介绍了猫鼬-获取_id列表,而不是具有_id的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行以下查询:

Group.find({program: {$in: [...]}}).lean().select('_id')

然后返回以下信息:

[{_id: ...}, {_id: ...}, {_id: ...}, {_id: ...}]

如下:

[..., ..., ..., ...] where ... represents an _id of a Group

当然,我可以只运行查询,然后遍历返回的组,但是如果可能的话,我想在查询中进行操作,因为这可能会更快.

Of course I could just run the query and then loop through the Groups I get back, but I would like to do it in the query if possible, because that's probably going to be faster.

谢谢你们!

推荐答案

Group.find({program: {$in: [...]}})
  .distinct('_id')

db.collection.distinct(字段,查询)

在单个集合中查找指定字段的不同值,并将结果返回到数组中.

Finds the distinct values for a specified field across a single collection and returns the results in an array.

了解详情.

这篇关于猫鼬-获取_id列表,而不是具有_id的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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