简体   繁体   English

mongoDb嵌入式查询以查找数据列表

[英]mongoDb embedded query to find list of data

I have the following embedded data And I am trying to get all the transactions which have a same week number for a particular saved account based on the accountId . 我具有以下嵌入式数据,并且我正在尝试根据accountId为特定的已保存帐户获取具有相同周数的所有交易。 I tried the following query but it returns an empty array. 我尝试了以下查询,但它返回一个空数组。

savingsAccount.find({accountId: "123456789012345678", transactions: {weekNumber: 32}})

Any Ideas? 有任何想法吗? Thanks so much in advance. 非常感谢。

    "savingsAccount":
    {
        "_id": "598cb8686739d49a6f59dbb4",
        "accountId": "123456789012345678",
        "__v": 2,
        "transactions": [
            {
                "_id": "598cb8a86739d49a6f59dbb5",
                "amount": 1234,
                "weekNumber": 32,
                "date": "2017-08-10T19:48:56.347Z"
            },
            {
                "_id": "598cbb70a4a8f89c18309d87",
                "amount": 1234,
                "weekNumber": 32,
                "date": "2017-08-10T20:00:48.241Z"
            }
        ]
    }

This ... 这个 ...

savingsAccount.find({
    accountId: "123456789012345678", 
    'transactions.weekNumber': 32
})

... will return all documents for which accountId is "123456789012345678" and the weekNumber attribute of at least one subdocument in the transactions array is 32. ...将返回其accountId为“ 123456789012345678”且transactions数组中至少一个子文档的weekNumber属性为32的所有文档。

Your original query returned no matches because the second predicate ( transactions: {weekNumber: 32} ) was attempting to match elements of the transactions array with the value: {weekNumber: 32} and, since that only represents part of a transactions document, no match was found. 您的原始查询未返回任何匹配项,因为第二个谓词( transactions: {weekNumber: 32} )正尝试匹配值为{weekNumber: 32}transactions数组的元素,并且由于该谓词仅表示交易文档的一部分 ,因此没有找到匹配项。

Edit 1 : if you want to go further than this to only return the subdocuments in the transactions array which have weekNumber=32 then you have to project and filter (available in versions >= 3.2). 编辑1 :如果您想做得比这更进一步,以仅返回transactions中具有weekNumber=32的子文档,则必须进行投影过滤 (在版本> = 3.2中可用)。 Here's an example: 这是一个例子:

db.savingsAccount.aggregate([
    {$match: { accountId: "123456789012345678" }},
    {$project: {
        accountId: 1,
        __v: 1,
        transactions: {$filter: {
            input: '$transactions',
            as: 't',
            cond: {$eq: ['$$t.weekNumber', 32]}
        }}
    }}
])

This will return all documents for which accountId is "123456789012345678" and the weekNumber attribute of at least one subdocument in the transactions array is 32 and for each matched document it will filter the transactions array such that only those subdocuments having weekNumber=32 are returned. 这将返回accountId为“ 123456789012345678”且transactions数组中至少一个子文档的weekNumber属性为32的所有文档,并且对于每个匹配的文档,它将过滤transactions数组,以便仅返回具有weekNumber=32那些子文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM