mongodb - mongo复杂查询问题
我想大声告诉你
我想大声告诉你 2017-05-02 09:25:22
0
4
750

原json

{
    "_id" : ObjectId("58b3a8dc96fbc7cfb8287093"),
    "name" : "《地下水质量标准》",
    "GBNumber" : "GB/T 14848-93",
    "dataEntryClerk" : "handsomeboy",
    "lastModified" : "Fri Feb 27 2017 12:03:40 GMT+0800",
    "contents" : [ 
        {
            "standardID" : "9527-01",
            "unit" : "mg/L",
            "classifications" : [ 
                {
                    "level" : "I",
                    "upperLimit" : "0",
                    "lowerLimit" : "50"
                }, 
                {
                    "level" : "II",
                    "upperLimit" : "50",
                    "lowerLimit" : "150"
                }, 
                {
                    "level" : "III",
                    "upperLimit" : "150",
                    "lowerLimit" : "250"
                }, 
                {
                    "level" : "IV",
                    "upperLimit" : "250",
                    "lowerLimit" : "350"
                }, 
                {
                    "level" : "V",
                    "upperLimit" : "350",
                    "lowerLimit" : "-1"
                }
            ]
        }, 
        {
            "standardID" : "7439-89-6",
            "unit" : "mg/L",
            "classifications" : [ 
                {
                    "level" : "I",
                    "upperLimit" : "0",
                    "lowerLimit" : "0.1"
                }, 
                {
                    "level" : "II",
                    "upperLimit" : "0.1",
                    "lowerLimit" : "0.2"
                }, 
                {
                    "level" : "III",
                    "upperLimit" : "0.2",
                    "lowerLimit" : "0.3"
                }, 
                {
                    "level" : "IV",
                    "upperLimit" : "0.3",
                    "lowerLimit" : "0.4"
                }, 
                {
                    "level" : "V",
                    "upperLimit" : "0.5",
                    "lowerLimit" : "-1"
                }
            ]
        }
    ]
}

想查询出来的结果:

{
    "contents" : [ 
        {
            "standardID" : "9527-01",
            "unit" : "mg/L",
            "classifications" : [ 
                {
                    "level" : "I",
                    "upperLimit" : "0",
                    "lowerLimit" : "50"
                }, 
                {
                    "level" : "II",
                    "upperLimit" : "50",
                    "lowerLimit" : "150"
                }, 
                {
                    "level" : "III",
                    "upperLimit" : "150",
                    "lowerLimit" : "250"
                }, 
                {
                    "level" : "IV",
                    "upperLimit" : "250",
                    "lowerLimit" : "350"
                }, 
                {
                    "level" : "V",
                    "upperLimit" : "350",
                    "lowerLimit" : "-1"
                }
            ]
        }
    ]
}

请问下各位大牛db.xxx.find() 该怎么写呢

我想大声告诉你
我想大声告诉你

reply all(4)
刘奇

First of all, a comment:

Ask a question and post minimizedexamples of reproducibleproblems. If you post such a long document, everyone will be tired of reading it.


What you need is actually to return specific documents in the array, not the entire array.

1 If you only need to return one element in the array

db.xxx.find(
    {'contents.standardID': '9527-01'},
    {contents: {$elemMatch: {standardID: '9527-01'}}, _id: 0}
)

The first line is the query condition, and the second line is the filter condition. You can see that operators can also be used in filter conditions. However, this operator will only return the first element that meets the condition, and requires MongoDB version 2.2 or above. Or use the

subscript selector:

db.xxx.find(
    {'contents.standardID': '9527-01'},
    {'contents.$': 1, _id: 0}
)
$The query conditions in the above example can also be used.

$elemMatchIf you need to return multiple matching elements in an array:

2

$unwindUse

to operate contents as an independent document stream. For the code, see @bguo's answer.

But if your array is large, this can cause performance issues. $unwind来把contents
3

$filterThis is a new operator in the

version, used to filter the returned content.

db.xxx.aggregate(
    {$match: {'contents.standardID': '9527-01'}},
    {$project: {
        contents: {$filter: {
            input: '$contents',
            as: 'contents',
            cond: {$eq: ['$$contents.standardID', '9527-01']}
        }},
        _id: 0
    }}
)
3.2Of course you can also use and many other methods.

为情所困

Query conditions. For example name

db.xxx.find({"name" : "《地下水质量标准》" } ,function(err, data){ 
    if(data){
        console.log(data.contents)
    }
})
给我你的怀抱

One way to implement it: Use MongoDB’s Aggregate.

1. First apply $unwind to the contents containing array

2. Then use $match to apply filtering conditions

3. Finally, use $project to retain the required fields

Refer to the code below

 db.test1.aggregate([
                    {$unwind : "$contents"},
                    {$match : { name :  "《地下水质量标准》", "contents.standardID" :  "9527-01"}},
                    { $project : { contents : 1 , _id : 0 }}
                    ])

For reference.

Love MongoDB! Have Fun!

------------------------Gorgeous separator--------------------- ----------

MongoDB Chinese community has many offline activities, please click below:

2017 Huashan Sword Discussion|MongoDB Chinese Community

Hangzhou Station is coming in March! ! ! Interested friends please sign up quickly! ! !

给我你的怀抱
db.xxx.find({"contents.standardID": "9527-01"}, {contents: 1})

_id字段会自动输出,就忽略吧
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!