Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB保留满足条件的路径_Mongodb_Mongodb Query - Fatal编程技术网

MongoDB保留满足条件的路径

MongoDB保留满足条件的路径,mongodb,mongodb-query,Mongodb,Mongodb Query,我是MongoDB的新手 在查找查询中,我使用以下结构: db.report.find({'accountList.transactionList.description': /.*aear.*/i}) 但是,accountList包含多个值,transaction list也包含多个值,确切的查询是: db.report.find({'accountList[0].transactionList[4].description': /.*aear.*/i}) 问题是accountList有多

我是MongoDB的新手

在查找查询中,我使用以下结构:

db.report.find({'accountList.transactionList.description': /.*aear.*/i})
但是,accountList包含多个值,transaction list也包含多个值,确切的查询是:

db.report.find({'accountList[0].transactionList[4].description': /.*aear.*/i})
问题是accountList有多个帐户,其中只有一个在描述中具有值“aear”。当我执行查询时,它会返回两个帐户,我只想保留描述中aear所在的帐户。此外,这必须适用于许多文件,因为它的文件具有不同的事务列表,因此在某些文档中,aear根本不会出现,而在其他文档中,它可能会出现多种类型,始终处于不同的位置。我认为必须在投影中做一些事情,但这样设置是不起作用的:

.projection({"accountList.id":1,"accountList.transactionList.description":1})
以下是输出:

"accountList" : [
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [
                {
                    "onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
                    "description" : "aear"
                },
                {
                    "onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
                    "description" : "bb"
                },
                {
                    "onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
                    "description" : "cc"
                }
            ]
        },
        {
            "id" : "2",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [
                {
                    "onDate" : ISODate("2019-08-15T21:00:00.000-03:00"),
                    "description" : "aa",
                },
                {
                    "onDate" : ISODate("2019-08-14T21:00:00.000-03:00"),
                    "description" : "ee"
                }
            ]
        }
    ]
我想要这样的东西,我只得到满足条件的路径:

"accountList" : [
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [
                {
                    "onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
                    "description" : "aear"
                },
请尝试以下查询:

db.report.find({'accountList[0].transactionList[4].description': { $regex: /.*aear.*/i} })
或-仅返回第一个匹配的文档:

db.report.find({'accountList[0].transactionList[4].description': /.*aear.*/i}).limit(1)

更新我的答案,因为此问题已更新为新的所需o/p:

新问题的答案:

    /* 1 */
{
    "accountList" : [ 
        {
            "id" : "1100",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : {
                "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                "description" : "aear"
            }
        }, 
        {
            "id" : "1200",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : {
                "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                "description" : "aear"
            }
        }
    ]
}

/* 2 */
{
    "accountList" : [ 
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : {
                "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                "description" : "aear"
            }
        }
    ]
}
/* 1 */
{
    "accountList" : [ 
        {
            "id" : "1100",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }, 
        {
            "id" : "1200",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}

/* 2 */
{
    "accountList" : [ 
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}

/* 3 */
{
    "accountList" : [ 
        {
            "id" : "00",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }, 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }, 
        {
            "id" : "100",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}
/* 1 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0c7"),
    "accountList" : [ 
        {
            "id" : "4474",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0d7"),
    "accountList" : [ 
        {
            "id" : "4400",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df077"),
    "accountList" : [ 
        {
            "id" : "0000",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 4 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df1c7"),
    "accountList" : [ 
        {
            "id" : "0101",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}
如果您只有一个交易符合给定的标准
/.*aear.*/i
,那么假设描述在报告文档的会计列表数组中是唯一的(与提供的示例完全相同):

但是,如果您有多个描述(跨报表文档accountsList数组中的多个对象)与accountList中的给定条件匹配:

db.report.aggregate([{
    $match: {
        'accountList.transactionList.description': /.*aear.*/i
    }
}, { $unwind: '$accountList' }, { $unwind: '$accountList.transactionList' }, { $match: { 'accountList.transactionList.description': /.*aear.*/i } },
{ $group: { _id: '$_id', accountList: { $push: '$accountList' }, data: { $first: '$$ROOT' } } }
    , { $addFields: { 'data.accountList': '$accountList' } }, { $replaceRoot: { 'newRoot': '$data' } }, { $project: { 'accountList': 1, _id: 0 } }
])
输出:

    /* 1 */
{
    "accountList" : [ 
        {
            "id" : "1100",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : {
                "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                "description" : "aear"
            }
        }, 
        {
            "id" : "1200",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : {
                "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                "description" : "aear"
            }
        }
    ]
}

/* 2 */
{
    "accountList" : [ 
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : {
                "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                "description" : "aear"
            }
        }
    ]
}
/* 1 */
{
    "accountList" : [ 
        {
            "id" : "1100",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }, 
        {
            "id" : "1200",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}

/* 2 */
{
    "accountList" : [ 
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}

/* 3 */
{
    "accountList" : [ 
        {
            "id" : "00",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }, 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }, 
        {
            "id" : "100",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}
/* 1 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0c7"),
    "accountList" : [ 
        {
            "id" : "4474",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0d7"),
    "accountList" : [ 
        {
            "id" : "4400",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df077"),
    "accountList" : [ 
        {
            "id" : "0000",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 4 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df1c7"),
    "accountList" : [ 
        {
            "id" : "0101",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}
如果您在事务数组和帐户的其他对象数组中有多个匹配描述(这也适用于上述所有场景,但根据需求可能不需要,它可能会很庞大,请检查输出中的文档#3以了解详情):

输出:

    /* 1 */
{
    "accountList" : [ 
        {
            "id" : "1100",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : {
                "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                "description" : "aear"
            }
        }, 
        {
            "id" : "1200",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : {
                "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                "description" : "aear"
            }
        }
    ]
}

/* 2 */
{
    "accountList" : [ 
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : {
                "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                "description" : "aear"
            }
        }
    ]
}
/* 1 */
{
    "accountList" : [ 
        {
            "id" : "1100",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }, 
        {
            "id" : "1200",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}

/* 2 */
{
    "accountList" : [ 
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}

/* 3 */
{
    "accountList" : [ 
        {
            "id" : "00",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }, 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }, 
        {
            "id" : "100",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}
/* 1 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0c7"),
    "accountList" : [ 
        {
            "id" : "4474",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0d7"),
    "accountList" : [ 
        {
            "id" : "4400",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df077"),
    "accountList" : [ 
        {
            "id" : "0000",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 4 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df1c7"),
    "accountList" : [ 
        {
            "id" : "0101",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}
如果要查找精确文本,也可以这样做(因为cond中不允许使用正则表达式):

输出:同上

回答老问题:

    /* 1 */
{
    "accountList" : [ 
        {
            "id" : "1100",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : {
                "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                "description" : "aear"
            }
        }, 
        {
            "id" : "1200",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : {
                "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                "description" : "aear"
            }
        }
    ]
}

/* 2 */
{
    "accountList" : [ 
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : {
                "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                "description" : "aear"
            }
        }
    ]
}
/* 1 */
{
    "accountList" : [ 
        {
            "id" : "1100",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }, 
        {
            "id" : "1200",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}

/* 2 */
{
    "accountList" : [ 
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}

/* 3 */
{
    "accountList" : [ 
        {
            "id" : "00",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }, 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }, 
        {
            "id" : "100",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}
/* 1 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0c7"),
    "accountList" : [ 
        {
            "id" : "4474",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0d7"),
    "accountList" : [ 
        {
            "id" : "4400",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df077"),
    "accountList" : [ 
        {
            "id" : "0000",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 4 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df1c7"),
    "accountList" : [ 
        {
            "id" : "0101",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}
好的,这里有两个选项,请尝试以下选项:

如果
accountList
中只有一个对象与给定的过滤器匹配,则可以简单地执行以下操作:

db.report.find({'accountList.transactionList.description': /.*aear.*/i}, {'accountList.$': 1})
输出:

    /* 1 */
{
    "accountList" : [ 
        {
            "id" : "1100",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : {
                "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                "description" : "aear"
            }
        }, 
        {
            "id" : "1200",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : {
                "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                "description" : "aear"
            }
        }
    ]
}

/* 2 */
{
    "accountList" : [ 
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : {
                "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                "description" : "aear"
            }
        }
    ]
}
/* 1 */
{
    "accountList" : [ 
        {
            "id" : "1100",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }, 
        {
            "id" : "1200",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}

/* 2 */
{
    "accountList" : [ 
        {
            "id" : "1",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}

/* 3 */
{
    "accountList" : [ 
        {
            "id" : "00",
            "type" : "xD",
            "currency" : "EUR",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }, 
                {
                    "onDate" : ISODate("2019-08-26T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }, 
        {
            "id" : "100",
            "type" : "xD",
            "currency" : "USD",
            "transactionList" : [ 
                {
                    "onDate" : ISODate("2019-08-16T00:00:00.000Z"),
                    "description" : "aear"
                }
            ]
        }
    ]
}
/* 1 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0c7"),
    "accountList" : [ 
        {
            "id" : "4474",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0d7"),
    "accountList" : [ 
        {
            "id" : "4400",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df077"),
    "accountList" : [ 
        {
            "id" : "0000",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 4 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df1c7"),
    "accountList" : [ 
        {
            "id" : "0101",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}
上面的
.find()
查询的缺点是,它只会在
accountList
中获取第一个匹配对象,如果您在
accountList
中有多个匹配对象用于给定的筛选器,那么您需要使用聚合(此聚合查询也可以用于早期场景,请检查输出是否存在差异):

输出:

// This first object is best example where you need aggregation

/* 1 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df1c7"),
    "accountList" : [ 
        {
            "id" : "0101",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }, 
        {
            "id" : "1111",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0d7"),
    "accountList" : [ 
        {
            "id" : "4400",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df077"),
    "accountList" : [ 
        {
            "id" : "0000",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

/* 4 */
{
    "_id" : ObjectId("5d6435145a0d22d3c86df0c7"),
    "accountList" : [ 
        {
            "id" : "4474",
            "transactionList" : [ 
                {
                    "description" : "aear"
                }, 
                {
                    "description" : "koe"
                }
            ]
        }
    ]
}

要实现这一点,您需要使用。我相信此代码将适用于您的情况:

db.report.aggregate([
  { "$match": { "accountList.transactionList.description": { $regex: "aear", $options: "i"} } },
  { "$unwind": "$accountList" },
  { "$unwind": "$accountList.transactionList" },
  { "$match": { "accountList.transactionList.description": { $regex: "aear", $options: "i"} } },
  { "$group": {
      "_id": {
          "_id": "$_id",
          "accountListId": "$accountList.id",
          "accountListType": "$accountList.type",
          "accountListCurrency": "$accountList.currency",
      },
      "transactionList": { "$push": "$accountList.transactionList" }
  }},
  { "$group": {
      "_id": "$_id._id",
      "accountList": {
          "$push": {
              "id": "$_id.accountListId",
              "type": "$_id.accountListType",
              "currency": "$_id.accountListCurrency",
              "transactionList": "$transactionList"
          }
      }
  }}
])

我对这项工作表示怀疑,因为要使它适用于许多文件,它不应该包含确切的位置
[0]
[4]
。谢谢你的回答!您确定在查询中返回JSON吗?它有重复的数据。您不应该在同一文档中有两个
id
transactionList
{“id”:“4474”,“transactionList”:[{“description”:“aear”},{“description”:“koe”}],“id”:“4475”,“transactionList”:[{“description”:“oho”},{“description”:“koe”}
。你能把一份文档的实际结构发布到收藏
报告中吗?当然,我会编辑我的答案的。在那里,我完成了编辑。谢谢。如果您没有找到解决方案,请检查我的答案是的,非常感谢!我明天就试试@塞利乌斯廷格:这些对你有用吗?还是有什么问题?这可能有点傻,但我对MongoDB完全是新手,我收到一条消息“消息”:“超出了$group的内存限制,但不允许外部排序。”。Pass allowDiskUse:选择加入为true。“然而,我在第二次尝试后得到了这个结果,第一次我设法得到了结果,但是我得到了完整的输出,而不是‘过滤’的输出。@CeliusStingher:您使用了哪个查询?您的$match是否按预期工作?在应用$match之后,您的数据集有多大?我使用了您在回答中提出的第二个代码。对不起,我一直要求答复。我被分配到一个完全不同的地方,我的注意力现在就在那里。考虑到解释和投入的精力,我对它投了赞成票,加上它可能是正确的,我在实现它时遇到了困难。谢谢,我没有尝试这个答案,因为aear可以以多种形式出现,应该用作正则表达式(aear,aear,aear,laearl,等等),太棒了!非常感谢,我刚刚将regex部分编辑为:{$regex:/.*aear.*/I}