我的MongoDB中有以下两个JSON对象
JSON - 1:
{
"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:
{
"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查询是:
db.getCollection('stories').find({
'projectId': 10003,
'changes.updates.fieldId': 'SprintData',
'changes.updates.from': '5',
'type': {'$in': ['Story', 'Task']}
})作为结果的一部分,我应该只在一个对象中获得JSON 2,其中有fieldId: SprintData和from:'5‘,没有什么不同。
我的蒙古问题应该是什么?我试过使用,但我不知道更多。
提前谢谢。
发布于 2020-05-26 11:24:03
您应该使用$elemMatch
$elemMatch运算符将包含数组字段的文档与至少一个匹配所有指定查询条件的元素匹配。
工作示例- https://mongoplayground.net/p/Y_BqBNX7yx0
db.getCollection('stories').find({
"projectId": 10003,
"changes.updates": {
$elemMatch: {
fieldId: "SprintData",
from: "5"
}
},
"type": {
"$in": [
"Story",
"Task"
]
}
})https://stackoverflow.com/questions/62020080
复制相似问题