mongodb subelement querying

后端 未结 3 1718
醉话见心
醉话见心 2021-01-19 15:38

here is the example

> db.test.insert({ name: \'test\', values: [ { check: true }, { check: false } ] })
> db.find({ values.check: true })[0]

相关标签:
3条回答
  • 2021-01-19 15:52
    > db.test.aggregate(
    { $unwind: "$values" },
    { $match: { "values.check": true } }
    ).result
    
    [
            {
                    "_id" : ObjectId("50e22046dc278908f3a38a8e"),
                    "name" : "test",
                    "values" : {
                            "check" : true
                    }
            }
    ]
    
    0 讨论(0)
  • 2021-01-19 15:59

    You can use the $ projection operator to include just the first values array element that matched the query:

     db.test.find({ 'values.check': true }, {name: 1, 'values.$': 1})
    

    returns:

    {
        "_id": ObjectId("50e22046dc278908f3a38a8e"), 
        "name": "test", 
        "values": [ { "check": true } ] }
    
    0 讨论(0)
  • 2021-01-19 16:08
    $fetchcode = "Fetch an array from the mongo db"
       foreach ($fetchcode as $mainkey => $mainVariable) {
            foreach($mainVariable as $key2 => $doc2)
            {
                if($key2 == "check"){
                    if($doc2 == "true")
                    {
                       //Your Function Here
                    }
                }
            }
        }
    

    You can try this

    0 讨论(0)
提交回复
热议问题