Maria Kari Maria Kari - 5 months ago 25
Node.js Question

How select data with given condition

I have following data.

{

"name" : "Maria",
"facebook" : [
{
"data" : "fb.com",
"privacy" : true
}
],
"twitter" : [
{
"data" : "twitter.com",
"privacy" : false
}
],
"google" : [
{
"data" : "google.com",
"privacy" : true
}
],
"phno" : [
{
"data" : "+1-1289741824124",
"privacy" : true
}
]
}


I want to return only data having privacy is equal to true. How do I do it ?

I tried but it returns all data having privacy is equal to false also. How do I query the data ?

Please post MongoDB query not Javascript code.

Thanks!

Answer

The way you could get this is using aggregation framework. As we have an array for each field, we need to unwind it first, then we can use $project to set field value or simply null. As this looks like a simple query, it could give a bit of trouble. The way we can improve that is change a document structure, to have an array of providers and simple providerType field.

Aggregation stages below:

db.maria.find()
var unwindFb = {
    $unwind : "$facebook"
}
var unwindtw = {
    $unwind : "$twitter"
}
var unwindgo = {
    $unwind : "$google"
}
var unwindph = {
    $unwind : "$phno"
}
var project = {
    $project : {
        _id : 1,
        name : 1, // list other fields here

        facebook : {
            $cond : {
                if  : {
                    $gte : ["$facebook.privacy", true]
                },
            then : [{
                    data : "$facebook.data",
                    privacy : "$facebook.privacy"
                }
            ],
            else  : null
        }
    },
    twitter : {
        $cond : {
            if  : {
                $gte : ["$twitter.privacy", true]
            },
        then : [{
                data : "$twitter.data",
                privacy : "$twitter.privacy"
            }
        ],
        else  : null
    }
},
google : {
    $cond : {
        if  : {
            $gte : ["$google.privacy", true]
        },
    then : [{
            data : "$google.data",
            privacy : "$google.privacy"
        }
    ],
    else  : null
}
},
phno : {
    $cond : {
        if  : {
            $gte : ["$phno.privacy", true]
        },
    then : [{
            data : "$phno.data",
            privacy : "$phno.privacy"
        }
    ],
    else  : null
}
}
}
}

db.maria.aggregate([unwindFb, unwindtw, unwindgo, unwindph, project])

then output looks like this:

{
    "_id" : ObjectId("575df49d31dcfb59af388e1a"),
    "name" : "Maria",
    "facebook" : [ 
        {
            "data" : "fb.com",
            "privacy" : true
        }
    ],
    "twitter" : null,
    "google" : [ 
        {
            "data" : "google.com",
            "privacy" : true
        }
    ],
    "phno" : [ 
        {
            "data" : "+1-1289741824124",
            "privacy" : true
        }
    ]
}

edit

having structure like this:

{
    "_id" : ObjectId("575e4c8731dcfb59af388e1d"),
    "name" : "Maria",
    "providers" : [ 
        {
            "type" : "facebook",
            "data" : "fb.com",
            "privacy" : true
        }, 
        {
            "type" : "twitter",
            "data" : "twitter.com",
            "privacy" : false
        }, 
        {
            "type" : "google",
            "data" : "google.com",
            "privacy" : true
        }, 
        {
            "type" : "phno",
            "data" : "+1-1289741824124",
            "privacy" : true
        }
    ]
}

with query like this:

db.maria.aggregate([{
            $unwind : "$providers"
        }, {
            $match : {
                "providers.privacy" : true
            }
        }, {
            $group : {
                _id : {
                    id : "$_id",
                    name : "$name"
                },
                providers : {
                    $push : "$providers"
                }
            }
        }, {
            $project : {
                _id : "$_id._id",
                name : "$_id.name",
                providers : 1
            }
        }
    ])

we are gaining dynamic output, and we don't need to take care about provider name as this is covered by generic structure

{
    "providers" : [ 
        {
            "type" : "facebook",
            "data" : "fb.com",
            "privacy" : true
        }, 
        {
            "type" : "google",
            "data" : "google.com",
            "privacy" : true
        }, 
        {
            "type" : "phno",
            "data" : "+1-1289741824124",
            "privacy" : true
        }
    ],
    "name" : "Maria"
}