Mongo查询问题-查询来自两个不同集合的记录并合并/合并为一个记录

时间:2018-11-27 08:58:19

标签: mongodb join collections mongodb-query aggregation-framework

我有两个collections,从我的查询中,我从这些集合中获得了记录,

Collection_A:

查询:

db.getCollection('Collection_A').findOne({"meta.md5":"270daaa8c9da2c0ce114fd297ecb3fce"},{"pdf":1,"_id":0})

记录:

{
    "pdf" : {
        "engine" : "pdf",
        "status" : "done",
        "retry" : 3,
        "limit" : 4500,
        "stop_on_detection" : 0,
        "file_type" : "*",
        "file_limit_slot" : 60,
        "start_time" : 1543225534.08531,
        "end_time" : 1543225534.90477,
        "detection" : true
    }
}

Collection_B:

查询:

db.getCollection('Collection_B').find({"md5":"270daaa8c9da2c0ce114fd297ecb3fce"},{"pdf":1,"_id":0})

记录:

{
    "pdf" : {
        "version" : {
            "decomposer" : "1.0"
        },
        "results" : {
            "status" : 1,
            "vector_value" : 2,
            "file_version" : "",
            "is_portmanteau" : false,
            "version" : {
                "decomposer" : "1.0"
            },
            "pdfid" : {
                "Encrypt" : 0,
                "stream" : 2,
                "JavaScript" : 1,
                "OpenAction" : 1,
                "JS" : 1,
                "header" : "%PDF-1.1",
                "entropy" : "",
                "totalEntropy" : "",
                "ObjStm" : 0,
                "AcroForm" : 0,
                "countEof" : "",
                "xref" : 1,
                "endobj" : 9,
                "filename" : "270daaa8c9da2c0ce114fd297ecb3fce.pdf",
                "version" : "0.2.1",
                "AA" : 0,
                "errorMessage" : "",
                "Launch" : 0,
                "RichMedia" : 0,
                "endstream" : 2,
                "nonStreamEntropy" : "",
                "JBIG2Decode" : 0,
                "trailer" : 1,
                "isPdf" : "True",
                "Page" : 1,
                "countCharAfterLastEof" : "",
                "dates" : {
                    "date" : []
                },
                "obj" : 9,
                "EmbeddedFile" : 1,
                "startxref" : 1,
                "XFA" : 0,
                "Colors" : 0,
                "streamEntropy" : "",
                "errorOccurred" : "False"
            },
            "file_id" : "ae9eb8ef543504666d2c4f58e1c2ff48",
            "file_path" : "NA",
            "pdf_id_error" : ""
        },
        "scan_duration" : 0.0714380741119385
    }
}

如何使用单个query合并这些集合中的这两个记录?在SQL中,我使用了join从两个不同的表中获取数据。由于我对mongo不太熟悉,因此不确定该如何实现?

预期结果:

    {
    "pdf" : {
        "status" : "Completed",
        "detection" : true,
    "pdf" : {
        "version" : {
            "decomposer" : "1.0"
        },
        "results" : {
            "status" : 1,
            "vector_value" : 2,
            "file_version" : "",
            "is_portmanteau" : false,
            "version" : {
                "decomposer" : "1.0"
            },
            "pdfid" : {
                "Encrypt" : 0,
                "stream" : 2,
                "JavaScript" : 1,
                "OpenAction" : 1,
                "JS" : 1,
                "header" : "%PDF-1.1",
                "entropy" : "",
                "totalEntropy" : "",
                "ObjStm" : 0,
                "AcroForm" : 0,
                "countEof" : "",
                "xref" : 1,
                "endobj" : 9,
                "filename" : "270daaa8c9da2c0ce114fd297ecb3fce.pdf",
                "version" : "0.2.1",
                "AA" : 0,
                "errorMessage" : "",
                "Launch" : 0,
                "RichMedia" : 0,
                "endstream" : 2,
                "nonStreamEntropy" : "",
                "JBIG2Decode" : 0,
                "trailer" : 1,
                "isPdf" : "True",
                "Page" : 1,
                "countCharAfterLastEof" : "",
                "dates" : {
                    "date" : []
                },
                "obj" : 9,
                "EmbeddedFile" : 1,
                "startxref" : 1,
                "XFA" : 0,
                "Colors" : 0,
                "streamEntropy" : "",
                "errorOccurred" : "False"
            },
            "file_id" : "ae9eb8ef543504666d2c4f58e1c2ff48",
            "file_path" : "NA",
            "pdf_id_error" : ""
        },
        "scan_duration" : 0.0714380741119385
    }
}
}

更新:

尝试过此查询:

db.getCollection('Collection_A').aggregate( [ { $match: {"meta.md5":"270daaa8c9da2c0ce114fd297ecb3fce"}}, { $lookup: {from: "Collection_B",localField: "meta.md5", foreignField: "md5", as: "pdf"}}, { $project: {"pdf.status":1,"pdf.detection":1,"_id":0}} ] )

但是我从rogo mongo客户端收到此错误

Failed to execute script.

Error:
Assert: command failed: {
    "ok" : 0,
    "errmsg" : "global.Collection_B cannot be sharded",
    "code" : 28769,
    "codeName" : "Location28769"
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
DBCollection.prototype.aggregate@:1:355
@(shell):1:1

Error: command failed: {
    "ok" : 0,
    "errmsg" : "global.Collection_B cannot be sharded",
    "code" : 28769,
    "codeName" : "Location28769"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
DBCollection.prototype.aggregate@:1:355
@(shell):1:1

0 个答案:

没有答案