Mongodb:不支持在$ convert中从数组到objectId的转换,没有onError值 [英] Mongodb: Unsupported conversion from array to objectId in $convert with no onError value
问题描述
我使用mongodb 4.0.5.我正在使用查找来加入两个集合,外键是位于request_by数组中的字符串值,另一个是 ObjectId
I use mongodb 4.0.5. I'm using lookup to join two collections, the foreign key is a string value located in request_by array and the other one is ObjectId
{
$addFields: {
convertedId: {
$toObjectId: "$request_by.userId"
}
}
}
我想将外键转换为ObjectId,以便我可以加入它们.但是它说不支持从$ convert中的数组到objectId的转换,没有onError值"
i want to convert the foreign key to ObjectId so i can join them. But it says "Unsupported conversion from array to objectId in $convert with no onError value"
我有一个类似这样的数据:
i have a data something like this:
Simulation collection
{
"_id": "8f361e8969948e1c435c06d7",
"request_by": [{
"userId": "ae83ccfa592f4963a395263c",
"iat": 1544801930,
"exp": 1544819930
}],
"status": "finish",
"start": "2018-12-14T15:39:29.588Z",
"end": "2018-12-14T16:59:29.538Z",
"duration": 80,
"passing_grade": 100,
"created_at": "2018-12-14T15:39:29.588Z",
"updated_at": "2018-12-14T15:43:12.897Z",
"__v": 0
}
如果我有这样的数据,我该如何加入他们?
How i can join them if i have data like that?
推荐答案
您需要 $ map ,因为request_by
是一个数组,因此您可以将该数组直接传递到$lookup
(连接单个字段或数组).
You need $map since request_by
is an array, then you can pass that array directly into $lookup
(joins single fields or arrays).
{
$addFields: {
convertedId: {
$map: {
input: "$request_by",
as: "r",
in: { $toObjectId: "$$r.userId" }
}
}
}
}
这篇关于Mongodb:不支持在$ convert中从数组到objectId的转换,没有onError值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!