Mongo Query Issue - запрашивает записи из двух разных коллекций и объединяет / объединяет в одну запись - PullRequest
0 голосов
/ 27 ноября 2018

У меня есть два 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
    }
}

Коллекция_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
    }
}

Как объединить эти обе записи из этих коллекций, используя один querySQL я использовал join для получения данных из двух разных таблиц.Так как я не очень знаком с монго, не знаете, как этого добиться?

Ожидаемый результат:

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

Но я получаю эту ошибку от robo 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
...