简体   繁体   English

如何在给定条件下选择数据

[英]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. 我只想返回具有隐私权的数据等于true。 How do I do it ? 我该怎么做 ?

I tried but it returns all data having privacy is equal to false also. 我试过了,但它返回的所有具有隐私的数据也等于false。 How do I query the data ? 如何查询数据?

Please post MongoDB query not Javascript code. 请发布MongoDB查询而不是Javascript代码。

Thanks! 谢谢!

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([{
            $project : {
                _id : 1,
                name : 1,
                "providers" : {
                    $filter : {
                        input : "$providers",
                        as : "p",
                        cond : {
                            $eq : ["$$p.privacy", true]
                        }
                    }
                }
            }
        }
    ])
    ])

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"
}

end of edit 编辑结束

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. 由于每个字段都有一个数组,因此我们需要先对其展开,然后可以使用$project设置字段值或直接将其设置为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. 我们可以改进的方法是更改​​文档结构,以具有提供程序数组和简单的providerType字段。

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
        }
    ]
}

If you are open to use lodash ...this is how you can do it... 如果您愿意使用lodash ...这就是您可以使用的方式...

    var tmp  = {
    ...you json here 
    }

    var res =[];//result array of filtered data
     _.forIn(tmp, function (o) {
                    if (Array.isArray(o)) {
                        if (o[0].privacy === true) {
                            res.push(o);
                        }
                    }
    });

you can use the below code to iterate the object and check for the privacy true 您可以使用以下代码来迭代对象并检查隐私是否为true

var list = {

"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
    }
]

} }

$.each(Object.keys(list), function(index,value){
  if(list[value][0].privacy)
  {
 console.log(list[value][0]);
 }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM