仅在猫鼬.find()上返回文档_id [英] only return document _id on mongoose .find()
问题描述
我通过将新数据推送到文档中的数组来每秒更新100多个文档.为了获得要添加数据的文档,我使用了mongoose .find().limit(1)
函数,并返回了整个文档.它工作正常.
I update 100's of documents every second by pushing new data to an array in its document. To get the document of which I am going to add data to, I use the mongoose .find().limit(1)
function, and return the whole document. It works fine.
为了帮助解决一些内存和cpu问题,我想知道如何让find()
只返回文档的id
,以便可以将其用于$ push或$ set新数据.
To help with some memory and cpu issues I have, I was wondering how I could get find()
to only return the id
of the document so I can use that to $push or $set new data.
谢谢.
推荐答案
You want to use Projection to tell your query exactly what you want off of your objects.
_id
,否则始终将其包括在内.
_id
is always included unless you tell it not to.
readings = await collection
.find({
name: "Some name you want"
})
.project({
_id: 1 // By default
})
.toArray();
这篇关于仅在猫鼬.find()上返回文档_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!