[英]Mongoose populate() returns the '_id' field of populated documents with "new ObjectId()"
So I'm trying to fetch a set of company member documents that correspond to a specific user, and populate the company field of each document in the process.所以我试图获取一组与特定用户相对应的公司成员文档,并在此过程中填充每个文档的公司字段。 The aim here is to get the _id and the name fields of each company document that corresponds to a company member.
这里的目的是获取与公司成员对应的每个公司文档的_id和name字段。 I've been successful in fetching the company member documents, however , the _id field of each populated company is returned together with the "new ObjectId()" constructor as shown below:
我已经成功获取公司成员文档,但是,每个填充公司的_id字段与“new ObjectId()”构造函数一起返回,如下所示:
The company members: [
{
company: { _id: new ObjectId("6296180fj2a538be87f058f"), name: 'aaa' }
}
]
Of course, the desired outcome is to get only the _id content without the ObjectId() constructor .当然,期望的结果是只获取 _id 内容而没有 ObjectId() 构造函数。 I've tried using the method suggested in the accepted answer of this StackOverflow post , however, I got the same results (meaning the ObjectId() constructor didn't go away):
我尝试使用此 StackOverflow 帖子的已接受答案中建议的方法,但是,我得到了相同的结果(意味着 ObjectId() 构造函数没有消失):
// Fetch up to 10 company members and populate their companies.
let companyMembers = await CompanyMember.find({user: user_id}, {_id: 0, company: 1}).populate({
path: 'company',
match: { name: { $regex : new RegExp(company_name, "i") } },
select: { _id: {"$toString": "$_id"}, name: 1 }
}).limit(10).exec();
你试过lean()
功能吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.