Shisoft
Shisoft

Reputation: 4245

How to retrieve a document by its own sub document or array?

I have such structure of document:

{  
    "_id" : "4e76fd1e927e1c9127d1d2e8",  
    "name" : "***",  
    "embedPhoneList" : [  
        {  
            "type" : "家庭",  
            "number" : "00000000000"  
        },  
        {  
            "type" : "手机",  
            "number" : "00000000000"  
        }  
    ],  
    "embedAddrList" : [  
        {  
            "type" : "家庭",  
            "addr" : "山东省诸城市***"  
        },  
        {  
            "type" : "工作",  
            "addr" : "深圳市南山区***"  
        }  
    ],  
    "embedEmailList" : [  
        {  
            "email" : "********@gmail.com"  
        },  
        {  
            "email" : "********@gmail.com"  
        },  
        {  
            "email" : "********@gmail.com"  
        },  
        {  
            "email" : "********@gmail.com"  
        }  
    ]  
} 

What I wan't to do is find the document by it's sub document,such as email in embedEmailList field. Or if I have structure like this

{  
    "_id" : "4e76fd1e927e1c9127d1d2e8",  
    "name" : "***",   
    "embedEmailList" : [  
        "[email protected]" ,  
        "********@gmail.com" ,  
        ]  
} 

the embedEmailList is array,how to find if there is [email protected]?

Thanks.

Upvotes: 0

Views: 308

Answers (1)

Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47913

To search for a specific value in an array, mongodb supports this syntax:

db.your_collection.find({embedEmailList : "[email protected]"});

See here for more information.

To search for a value in an embedded object, it supports this syntax:

db.your_collection.find({"embedEmailList.email" : "[email protected]"});

See here for more information.

Upvotes: 2

Related Questions