Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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
Python mongo选择文档属性的所有唯一组合_Python_Mongodb - Fatal编程技术网

Python mongo选择文档属性的所有唯一组合

Python mongo选择文档属性的所有唯一组合,python,mongodb,Python,Mongodb,如果我收集了以下文件: {a:'1', b:'2', c:'3', d:'a'}, {a:'1', b:'2', c:'1', d:'b'}, {a:'1', b:'2', c:'3', d:'c'}, {a:'1', b:'3', c:'1', d:'d'}, {a:'1', b:'2', c:'3', d:'e'}, {a:'2', b:'2', c:'1', d:'f'} 要获得属性a、b、c的所有唯一组合,最有效的mongo查询是什么?可能吗?结果如下所示: {a:'1', b:'2'

如果我收集了以下文件:

{a:'1', b:'2', c:'3', d:'a'},
{a:'1', b:'2', c:'1', d:'b'},
{a:'1', b:'2', c:'3', d:'c'},
{a:'1', b:'3', c:'1', d:'d'},
{a:'1', b:'2', c:'3', d:'e'},
{a:'2', b:'2', c:'1', d:'f'}
要获得属性a、b、c的所有唯一组合,最有效的mongo查询是什么?可能吗?结果如下所示:

{a:'1', b:'2', c:'3', d:*},
{a:'1', b:'2', c:'1', d:*},
{a:'1', b:'3', c:'1', d:*},
{a:'2', b:'2', c:'1', d:*}
*不在乎


谢谢,非常感谢您的帮助。

您可以试试这样的

db.<your collection>.aggregate([{$group:{"a": "$a", "b": "$b", "c": "$c"}}])
也请尝试此链接

我不是MongoDB专业人士,但我觉得你也可以使用js函数,比如:

a = {}
db.<your collection>.find().forEach(
    function(x)
    {
        if (a[[x.a, x.b, x.c]] == null) {
            a[[x.a, x.b, x.c]] = true;
            printjson(x);
        }
    }
)

这种操作的常用工具是DISTINCE函数。不幸的是,MongoDB的distinct只能过滤一个字段,例如db.collection.distincta。可以进行各种变通,例如使用聚合或组

例如:

你必须从结果中提取信息,但我想这不是一个大问题

例如:

db.collection.aggregate({
    $group: {
        _id: {
            a: "$a", 
            b: "$b", 
            c: "$c"
        }
    }
})
db.collection.group({
    key: {
        a: 1, 
        b: 1,
        c: 1
    }, 
    reduce: function (current, result) {}, 
    initial: {}
})