Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 子文档数组包含公共数据时返回文档的Mongo查询_Mongodb - Fatal编程技术网

Mongodb 子文档数组包含公共数据时返回文档的Mongo查询

Mongodb 子文档数组包含公共数据时返回文档的Mongo查询,mongodb,Mongodb,我正在尝试编写一个mongo查询,以返回来自cat1和cat2源的具有完全相同电子邮件的文档 请帮忙。我正在包含一个样本集合架构,其中包含预期匹配的文档 样本采集 { "id" : "1", "name" : "John", "contacts" : [ { "type" : "email", "source": "cat

我正在尝试编写一个mongo查询,以返回来自cat1和cat2源的具有完全相同电子邮件的文档

请帮忙。我正在包含一个样本集合架构,其中包含预期匹配的文档

样本采集

{
        "id" : "1",
        "name" : "John",
        "contacts" : [
                {
                        "type" : "email",
                        "source": "cat1",
            "detail": aa@yahoo.com
                },
                {
                        "type" : "email",
                        "source": "cat1",
            "detail": john@yahoo.com
                },
                {
                        "type" : "email",
                        "source": "cat2",
            "detail": john@yahoo.com
                }
        ]
},
{
        "id" : "2",
        "name" : "Bell",
        "contacts" : [
                {
                        "type" : "email",
                        "source": "cat1",
            "detail": jj@yahoo.com
                },
                {
                        "type" : "email",
                        "source": "cat2",
            "detail": john@yahoo.com
                }
        ]
},

{
        "id" : "3",
        "name" : "Sam",
        "contacts" : [
                {
                        "type" : "email",
                        "source": "cat1",
            "detail": exmaple@yahoo.com
                },
                {
                        "type" : "email",
                        "source": "cat3",
            "detail": exmaple@yahoo.com
                }
        ]
}
预期结果如下,因为这是具有公共电子邮件地址的文档john@yahoo.com来自源cat1和cat2

{
        "id" : "1",
        "name" : "John",
        "contacts" : [
                {
                        "type" : "email",
                        "source": "cat1",
            "detail": aa@yahoo.com
                },
                {
                        "type" : "email",
                        "source": "cat1",
            "detail": john@yahoo.com
                },
                {
                        "type" : "email",
                        "source": "cat2",
            "detail": john@yahoo.com
                }
        ]
}
> db.collection.find().forEach(function(doc) {
        var cat1Email = new Set(); 
        var cat2Email = new Set(); 

        doc.contacts.forEach(function(c){
            if (c.source === 'cat1'){
                // save the cat1 emails
                cat1Email.add(c.detail); 
                if (cat2Email.has(c.detail)){
                    // if there is same email from cat2, print this document 
                    return printjson(doc);
                }
            }else if(c.source === 'cat2'){
                // save cat2 email
                cat2Email.add(c.detail); 
                if (cat1Email.has(c.detail)){
                    // if there is same email from cat1, print this document
                    return printjson(doc)
                }
            }
        }); 
    })  
不应返回id为2的文档,因为虽然它们都有来自cat1和cat2源的电子邮件,但电子邮件地址不同

不应返回id为3的文档,因为通过电子邮件地址,它们的类别不是cat1和cat2

{
        "id" : "1",
        "name" : "John",
        "contacts" : [
                {
                        "type" : "email",
                        "source": "cat1",
            "detail": aa@yahoo.com
                },
                {
                        "type" : "email",
                        "source": "cat1",
            "detail": john@yahoo.com
                },
                {
                        "type" : "email",
                        "source": "cat2",
            "detail": john@yahoo.com
                }
        ]
}
> db.collection.find().forEach(function(doc) {
        var cat1Email = new Set(); 
        var cat2Email = new Set(); 

        doc.contacts.forEach(function(c){
            if (c.source === 'cat1'){
                // save the cat1 emails
                cat1Email.add(c.detail); 
                if (cat2Email.has(c.detail)){
                    // if there is same email from cat2, print this document 
                    return printjson(doc);
                }
            }else if(c.source === 'cat2'){
                // save cat2 email
                cat2Email.add(c.detail); 
                if (cat1Email.has(c.detail)){
                    // if there is same email from cat1, print this document
                    return printjson(doc)
                }
            }
        }); 
    })  

请尝试使用普通JavaScript代码来过滤来自cat1和cat2的相同电子邮件

{
        "id" : "1",
        "name" : "John",
        "contacts" : [
                {
                        "type" : "email",
                        "source": "cat1",
            "detail": aa@yahoo.com
                },
                {
                        "type" : "email",
                        "source": "cat1",
            "detail": john@yahoo.com
                },
                {
                        "type" : "email",
                        "source": "cat2",
            "detail": john@yahoo.com
                }
        ]
}
> db.collection.find().forEach(function(doc) {
        var cat1Email = new Set(); 
        var cat2Email = new Set(); 

        doc.contacts.forEach(function(c){
            if (c.source === 'cat1'){
                // save the cat1 emails
                cat1Email.add(c.detail); 
                if (cat2Email.has(c.detail)){
                    // if there is same email from cat2, print this document 
                    return printjson(doc);
                }
            }else if(c.source === 'cat2'){
                // save cat2 email
                cat2Email.add(c.detail); 
                if (cat1Email.has(c.detail)){
                    // if there is same email from cat1, print this document
                    return printjson(doc)
                }
            }
        }); 
    })  

什么意思?两个文档都有具有相同“cat”源的数组,那么为什么只返回一个文档呢?这是否意味着代表两者的“交叉点”?唯一相交的元素不是“cat1”数据吗?您是否期望从集合中获得所有“交叉点”?或者您只是对第一个文档的“交叉点”感兴趣?欢迎来到StackOverflow。当你向同龄人提问时,你需要比这更清楚。你的问题标题似乎与你的问题内容不同。若要满足您的问题内容,请尝试使用
>db.collection.find({$and:[{'contacts.source':'cat1'},{'contacts.source':'cat2'}]})
通过
$and
操作符进行查找。@zangw这并没有比较cat1和cat2电子邮件地址是同一电子邮件地址的事实。谢谢,伙计。实际上,我正试图找到一个解决方案,从.NET级别使用它时,它会更加健壮。我对Mongo相当陌生,但我在想是否需要使用聚合框架来实现这一点。@Raki,我试图通过聚合来解决它,但我失败了。希望有人通过汇总指出。