如何在给定条件下选择数据 [英] How select data with given condition

查看:89
本文介绍了如何在给定条件下选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据.

{

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

我只想返回具有隐私权的数据等于true.我该怎么办?

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

我尝试过,但它返回的所有具有隐私权的数据也等于false.如何查询数据?

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

请发布MongoDB查询而不是Javascript代码.

Please post MongoDB query not Javascript code.

谢谢!

推荐答案

编辑

具有这样的结构:

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

具有这样的查询:

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

编辑结束

获得此信息的方法是使用聚合框架. 由于每个字段都有一个数组,因此我们需要先对其展开,然后可以使用$project设置字段值或直接将其设置为null.由于这看起来像一个简单的查询,因此可能会带来一些麻烦.我们可以改进的方法是更改​​文档结构,以具有提供程序数组和简单的providerType字段.

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. 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.

以下汇总阶段:

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

然后输出如下:

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

这篇关于如何在给定条件下选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆