首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从MongoDB查询嵌套字典数组的嵌套数组

从MongoDB查询嵌套字典数组的嵌套数组
EN

Stack Overflow用户
提问于 2020-05-26 10:31:29
回答 1查看 357关注 0票数 0

我的MongoDB中有以下两个JSON对象

JSON - 1:

代码语言:javascript
复制
{
"projectId" : 10003,
"boardId" : 2,
"sprintId" : 3,
"epicId" : 10018,
"storyId" : 10066,
"name" : "Landing Screen",
"status" : "Closed",
"state" : "Closed",
"estimate" : 5,
"estimatedTime" : 0,
"jiraUpdatedDate" :1580208341294,
"changes" : [ 
    {
        "changeId" : "24623",
        "changeCreatedDateTime" :1580208341304,
        "updates" : [ 
            {
                "field" : "status",
                "fieldtype" : "jira",
                "fieldId" : "status",
                "from" : "5",
                "fromString" : "Resolved",
                "to" : "6",
                "toString" : "Closed"
            }
        ],
        "authorIsDeleted" : "true",
        "authorState" : "Inactive",
        "authorUsername" : null,
        "authorFullName" : "",
        "authorShortName" : null
    }, 
    {
        "changeId" : "12755",
        "changeCreatedDateTime" :1576222479010,
        "updates" : [ 
            {
                "field" : "assignee",
                "fieldtype" : "jira",
                "fieldId" : "assignee",
                "from" : "asfasfasfasf",
                "fromString" : "Girish",
                "to" : "bfbasgafasfas",
                "toString" : "Animesh"
            }
        ],
        "authorID" : "asfsafsafsas",
        "authorIsDeleted" : "false",
        "authorState" : "Active",
        "authorUsername" : null,
        "authorFullName" : "Harish",
        "authorShortName" : null
    }, 
    {
        "changeId" : "10795",
        "changeCreatedDateTime" : 1575028807038,
        "updates" : [ 
            {
                "field" : "Sprint",
                "fieldtype" : "custom",
                "fieldId" : "SprintData",
                "from" : "",
                "fromString" : "",
                "to" : "3",
                "toString" : "Sprint 2"
            }
        ],
        "authorID" : "bfbasgafasfas",
        "authorIsDeleted" : "true",
        "authorState" : "Inactive",
        "authorUsername" : null,
        "authorFullName" : "Animesh",
        "authorShortName" : null
    }, 
    {
        "changeId" : "10794",
        "changeCreatedDateTime" : 1575028793243,
        "updates" : [ 
            {
                "field" : "summary",
                "fieldtype" : "jira",
                "fieldId" : "summary",
                "from" : null,
                "fromString" : "Landing Screen - Enhancement",
                "to" : null,
                "toString" : "Landing Screen"
            }
        ],
        "authorID" : "bfbasgafasfas",
        "authorIsDeleted" : "true",
        "authorState" : "Inactive",
        "authorUsername" : null,
        "authorFullName" : "Animesh",
        "authorShortName" : null
    }
],
"type" : "Story",
"typeId" : 10003,
"createdDate" : 1589984431264
}

JSON - 2:

代码语言:javascript
复制
{
"projectId" : 10003,
"boardId" : 2,
"sprintId" : 11,
"epicId" : null,
"storyId" : 12202,
"name" : "MarTech Fixes",
"status" : "In Progress",
"state" : "In Progress",
"estimate" : 2,
"estimatedTime" : 0,
"jiraUpdatedDate" : 1589864759510,
"changes" : [ 
    {
        "changeId" : "51255",
        "changeCreatedDateTime" : 1589864759514,
        "updates" : [ 
            {
                "field" : "Fix Version",
                "fieldtype" : "jira",
                "fieldId" : "fixVersions",
                "from" : null,
                "fromString" : null,
                "to" : "10194",
                "toString" : "1.0.8(4)"
            }
        ],
        "authorID" : "asfasfasf",
        "authorIsDeleted" : "false",
        "authorState" : "Active",
        "authorUsername" : null,
        "authorFullName" : "ankit",
        "authorShortName" : null
    }, 
    {
        "changeId" : "40223",
        "changeCreatedDateTime" : 1585661512213,
        "updates" : [ 
            {
                "field" : "Sprint",
                "fieldtype" : "custom",
                "fieldId" : "SprintData",
                "from" : "5",
                "fromString" : "Sprint 4",
                "to" : "6",
                "toString" : "Sprint 5"
            }
        ],
        "authorID" : "bfbasgafasfas",
        "authorIsDeleted" : "true",
        "authorState" : "Inactive",
        "authorUsername" : null,
        "authorFullName" : "Animesh",
        "authorShortName" : null
    }, 
    {
        "changeId" : "40045",
        "changeCreatedDateTime" : 1585636672629,
        "updates" : [ 
            {
                "field" : "Rank",
                "fieldtype" : "custom",
                "fieldId" : "customfield_10019",
                "from" : "",
                "fromString" : "",
                "to" : "",
                "toString" : "Ranked higher"
            }
        ],
        "authorID" : "bfbasgafasfas",
        "authorIsDeleted" : "true",
        "authorState" : "Inactive",
        "authorUsername" : null,
        "authorFullName" : "Animesh",
        "authorShortName" : null
    }
],
"type" : "Task",
"typeId" : 10004,
"createdDate" : 1589984431264
}

我的MongoDB查询是:

代码语言:javascript
复制
db.getCollection('stories').find({
'projectId': 10003, 
'changes.updates.fieldId': 'SprintData', 
'changes.updates.from': '5', 
'type': {'$in': ['Story', 'Task']}
})

作为结果的一部分,我应该只在一个对象中获得JSON 2,其中有fieldId: SprintData和from:'5‘,没有什么不同。

我的蒙古问题应该是什么?我试过使用,但我不知道更多。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-26 11:24:03

您应该使用$elemMatch

$elemMatch运算符将包含数组字段的文档与至少一个匹配所有指定查询条件的元素匹配。

工作示例- https://mongoplayground.net/p/Y_BqBNX7yx0

代码语言:javascript
复制
db.getCollection('stories').find({
  "projectId": 10003,
  "changes.updates": {
    $elemMatch: {
      fieldId: "SprintData",
      from: "5"
    }
  },
  "type": {
    "$in": [
      "Story",
      "Task"
    ]
  }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62020080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档