我使用mongodb 4.0.5。我正在使用查找来加入两个集合,外键是位于request_by数组中的字符串值,另一个是 ObjectId
{
$addFields: {
convertedId: {
$toObjectId: "$request_by.userId"
}
}
}
我想将外键转换为ObjectId,以便我可以加入它们。但是它说:“在不支持onError值的$ convert中,从数组到objectId的不支持的转换”
我有类似这样的数据:
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
}
如果我有这样的数据,我该如何加入他们?
答案 0 :(得分:1)
由于<div class="item">
<div class="header colour-animate">Title 1</div>
<div class="description colour-animate">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet ante malesuada, aliquam libero sit amet, varius lectus.</p>
</div>
</div>
<svg width="100%" height="100px" viewBox="0 0 1000 100" preserveAspectRatio="none">
<path fill="none" stroke="#ffffff" stroke-width="2" d="M01000 0 L1000 50 L500 50 L 510 30 L510 70 L500 50 L 0 50 L0 100" />
<path fill="none" stroke="#ffd000" stroke-width="2" d="M01000 0 L1000 50 L500 50 L 510 30 L510 70 L500 50 L 0 50 L0 100" class="scroll-animate" />
</svg>
<div class="item right">
<div class="header colour-animate left">Title 2</div>
<div class="description colour-animate">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet ante malesuada, aliquam libero sit amet, varius lectus.</p>
</div>
</div>
<svg width="100%" height="100px" viewBox="0 0 1000 100" preserveAspectRatio="none">
<path fill="none" stroke="#ffffff" stroke-width="2" d="M01000 0 L1000 50 L500 50 L 510 30 L510 70 L500 50 L 0 50 L0 100" />
<path fill="none" stroke="#ffd000" stroke-width="2" d="M01000 0 L1000 50 L500 50 L 510 30 L510 70 L500 50 L 0 50 L0 100" class="scroll-animate" />
</svg>
<div class="item">
<div class="header colour-animate">Title 3</div>
<div class="description colour-animate">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet ante malesuada, aliquam libero sit amet, varius lectus.</p>
</div>
</div>
<svg width="100%" height="100px" viewBox="0 0 1000 100" preserveAspectRatio="none">
<path fill="none" stroke="#ffffff" stroke-width="2" d="M01000 0 L1000 50 L500 50 L 510 30 L510 70 L500 50 L 0 50 L0 100" />
<path fill="none" stroke="#ffd000" stroke-width="2" d="M01000 0 L1000 50 L500 50 L 510 30 L510 70 L500 50 L 0 50 L0 100" class="scroll-animate" />
</svg>
<div class="item right">
<div class="header colour-animate left">Title 4</div>
<div class="description colour-animate">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet ante malesuada, aliquam libero sit amet, varius lectus.</p>
</div>
</div>
<svg width="100%" height="100px" viewBox="0 0 1000 100" preserveAspectRatio="none">
<path fill="none" stroke="#ffffff" stroke-width="2" d="M01000 0 L1000 50 L500 50 L 510 30 L510 70 L500 50 L 0 50 L0 100" />
<path fill="none" stroke="#ffd000" stroke-width="2" d="M01000 0 L1000 50 L500 50 L 510 30 L510 70 L500 50 L 0 50 L0 100" class="scroll-animate" />
</svg>
<div class="item">
<div class="header colour-animate">Title 5</div>
<div class="description colour-animate">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet ante malesuada, aliquam libero sit amet, varius lectus.</p>
</div>
</div>
是一个数组,因此您需要$map,然后可以将该数组直接传递到request_by
中(加入单个字段或数组)。
$lookup