[英]only return document _id on mongoose .find()
I update 100's of documents every second by pushing new data to an array in its document.通过将新数据推送到文档中的数组,我每秒更新 100 个文档。 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.为了获取我要添加数据的文档,我使用.find().limit(1)
函数,并返回整个文档。 It works fine.它工作正常。
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.为了帮助解决我遇到的一些内存和 cpu 问题,我想知道如何让find()
只返回文档的id
,以便我可以使用它来 $push 或 $set 新数据。
Thanks.谢谢。
You want to use Projection to tell your query exactly what you want off of your objects.您想使用Projection准确地告诉您的查询您想要从对象中得到什么。
_id
is always included unless you tell it not to. _id
总是包括在内,除非你告诉它不要。
readings = await collection
.find({
name: "Some name you want"
})
.project({
_id: 1 // By default
})
.toArray();
You could use the distinct
method in order to get an array of _id
for your query.您可以使用distinct
方法为您的查询获取_id
数组。
Follow this question关注这个问题
正如@alexmac 所提到的,这对我有用:
collection.find({}, '_id')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.