Mongoose - 使用_id获取_ids列表而不是对象数组

mol*_*rat 10 mongoose mongodb node.js

我想运行以下查询:

Group.find({program: {$in: [...]}}).lean().select('_id')
Run Code Online (Sandbox Code Playgroud)

然后不要回复:

[{_id: ...}, {_id: ...}, {_id: ...}, {_id: ...}]
Run Code Online (Sandbox Code Playgroud)

但是:

[..., ..., ..., ...] where ... represents an _id of a Group
Run Code Online (Sandbox Code Playgroud)

当然我可以运行查询,然后遍历我回来的组,但我想在查询中尽可能这样做,因为这可能会更快.

感谢你们!

bar*_*sny 30

Group.find({program: {$in: [...]}})
  .distinct('_id')
Run Code Online (Sandbox Code Playgroud)

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

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

阅读更多.