只在mongoose .find()返回文件_id

通过将新数据推送到文档中的数组,我每秒更新100个文档。 为了得到我要添加数据的文档,我使用.find().limit(1)函数,并返回整个文档。 它工作正常。

为了帮助我解决一些内存和CPU问题,我想知道如何让find()只返回文档的id ,以便我可以使用$ push或$设置新的数据。

谢谢。

你想使用投影来准确地告诉你的查询你想要的对象。

总是包含_id除非你不告诉它。

 readings = await collection .find({ name: "Some name you want" }) .project({ _id: 1 // By default }) .toArray();