gpt4 book ai didi

mongodb - 项目匹配数组中的字段值

转载 作者:可可西里 更新时间:2023-11-01 10:14:32 24 4
gpt4 key购买 nike

我想更改文档结构,只显示 definition 数组中与文档中的 idLanguage 匹配的定义。我该怎么做?

具有 3 个元素的 definition 数组的文档示例(idLanguage 的三个不同 id):

{
"_id" : ObjectId("59bc29897d7934a6a7577ee0"),
"reference" : "FIIG=A23900 INC=62356",
"idTerm" : "0161-1#TM-218801#1",
"idLanguage" : "0161-1#LG-000002#1",
"statusTerm" : 0,
"idOrganisation" : "0161-1#OG-000194#1",
"idConcept" : "0161-1#01-000001#1",
"term" : "ZRYCHLOVAC ZçVERU, KE KULOMETU ",
"definition" : [
{
"_id" : ObjectId("59bc0bd77d7934a6a7243f05"),
"reference" : "FIIG=A23900 INC=62356",
"idDefinition" : "0161-1#DF-000001#1",
"idLanguage" : "0161-1#LG-000001#1",
"statusDefinition" : 0,
"idOrganisation" : "0161-1#OG-002462#1",
"definition" : "A metallic claw shaped pivoting item, designed to accelerate the weapon's recovery from recoil by assisting in realigning the breech with the barrel.",
"idConcept" : "0161-1#01-000001#1"
},
{
"_id" : ObjectId("59bc29047d7934a6a7370782"),
"reference" : "FIIG=A23900 INC=62356",
"idDefinition" : "0161-1#DF-283090#1",
"idLanguage" : "0161-1#LG-000002#1",
"statusDefinition" : 0,
"idOrganisation" : "0161-1#OG-000194#1",
"definition" : "Kovov‡ otocn‡ p‡kov‡ polo_ka pro zrychlov‡n’ obnoven’ stavu zbrane pred zpetn_m r‡zem /v_strelem t’m, _e napom‡h‡ osov_mu ztoto_nen’ z‡vorn’ku /z‡veru s hlavn’.",
"idConcept" : "0161-1#01-000001#1"
},
{
"_id" : ObjectId("59bc290b7d7934a6a73ce124"),
"reference" : "FIIG=A23900 INC=62356",
"idDefinition" : "0161-1#DF-668740#1",
"idLanguage" : "0161-1#LG-000005#1",
"statusDefinition" : 0,
"idOrganisation" : "0161-1#OG-000200#1",
"definition" : "Metalowy element wahliwy w ksztalcie szpona, przeznaczony do przyspieszenia powrotu broni po odrzucie poprzez wspomaganie ponownego ustawienia w linii zamka i lufy.",
"idConcept" : "0161-1#01-000001#1"
}
]
}

最佳答案

您可以使用 $indexOfArray$arrayElemAt以匹配值。较早的问题表明您至少使用 MongoDB 3.4,所以这应该不是问题:

db.collection.aggregate([
{ "$addFields": {
"definition": {
"$arrayElemAt": [
"$definition.definition",
{ "$indexOfArray": [
"$definition.idLanguage",
"$idLanguage"
}}
]
}
}}
])

idLanguage匹配的位置,通过"definition"(字段)从数组中提取。因此,您将用匹配这些属性的奇异值替换“数组”。

关于mongodb - 项目匹配数组中的字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46278469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com