Mongodb内嵌数组的完全匹配查询

样例数据:


    "cNo" : "11",
    "Details" : [
        {
             "dDate" : ISODate("2017-04-01T00:00:00.000+0800"),
             "bNo" : "No00000000497"
        },
        {
             "dDate" : ISODate("2017-05-01T00:00:00.000+0800"),
             "bNo" : "No00000000497"
        },
        {
            "dDate" : ISODate("2017-06-01T00:00:00.000+0800"),
             "bNo" : "No00000000497"
        }
    ]
}
{
    "cNo" : "12",    
    "Details" : [
        {
            "dDate" : ISODate("2017-04-01T00:00:00.000+0800"),
            "bNo" : "No00000000497"
        },
        {
            "dDate" : ISODate("2017-05-01T00:00:00.000+0800"),
            "bNo" : "No00000000497"
        },
        {
            "dDate" : ISODate("2017-06-01T00:00:00.000+0800"),
            "bNo" : "No00000000500"
        }
    ]
}

    "cNo" : "13",
    "Details" : [
        {
            "dDate" : ISODate("2017-04-01T00:00:00.000+0800"),
            "bNo" : "No00000000497"
        },
        {
            "dDate" : ISODate("2017-05-01T00:00:00.000+0800"),
            "bNo" : "No00000000497"
        },
         {
            "dDate" : ISODate("2017-06-01T00:00:00.000+0800"),
            "bNo" : "No00000000497"
        }
    ]
}

查询条件:dDate等于2017-06-01,且bNo等于No00000000497的数据

正确查询语句:{Details:{ $elemMatch:{ "bNo":"No00000000497","dDate":  ISODate("2017-10-01T00:00:00.000+0800") } }}

查询结果:11、13

(非完全匹配/错误)查询语句:{"Details.bNo":"No00000000497","Details.dDate":ISODate("2017-10-01T00:00:00.000+0800")}

查询结果:11、12、13

转载于:https://www.cnblogs.com/94cool/p/8310087.html

发布了153 篇原创文章 · 获赞 48 · 访问量 17万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

如何将数组中存在的值与mongo中的字段进行匹配

03-05

<div class="post-text" itemprop="text"> <p>I have this data set in mongo</p> <p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>{ _id: { question_id: 3 }, data: { answer: 11, count: 114, result: [ { _id: 3, question: "What is your age group?", options: [ { id: 10, option: "<18" }, { id: 11, option: "18-24" }, { id: 12, option: "24-35" }, { id: 13, option: ">35" } ], type: "Radio" } ] } },</code></pre> </div> </div> <p>Here i would like to perform an <code>equals to</code> check between <code>answer</code> and <code>result.options.id</code> and if the value matches it should return <code>result.options.option</code> </p> <p>So that the desired result is </p> <p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>{ _id: { question_id: 3 }, data: { answer:'18-24', count: 114, } },</code></pre> </div> </div> <p>So far i have tried this </p> <p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>'$project'=>[ 'res'=>[ '$map'=>[ 'input'=>'$data.result.options', 'as'=>'dt', 'in'=>[ '$cond'=>[ 'if'=>[ '$eq'=>[ '$$dt.id', '$data.answer' ] ], 'then'=>'$$dt.option', 'else'=>'$$dt.id' ] ] ] ] ]</code></pre> </div> </div> <p>Any clue as to what i am doing wrong . My code always returns the else statement</p> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览