mongoexport exceeded memory limit for $group, but did't allow external sort. pass allowDiskUse:true to opt in

480 views Asked by At

I'm trying to export a collection that I created with an aggregating query. This is the request text that I used to create the collection. If I run this query with aggregate instead of runCommand . At the end, I must add { allowDiskUse: true }, otherwise the request also gets an error exceeded memory limit for $group

db.runCommand({ 
   create: "tests",
   viewOn: "Nero Objects",
   pipeline:[
{ $match : { "RegistryRecord.IsExcluded": false, 
    "RegistryRecord.RegistryType": "Federal"
       }},
{$lookup: {
       from: 'Requests',
       localField: "CurrentRequestId",
       foreignField: "_id",
       as: "Requests"
}},
{$unwind: "$Requests"},
{ $project: {
    RegistryRecord:"$RegistryRecord.Code",
    EmissionObject:"$Summary.EmissionObject.Name",
    Organization:"$Summary.Organization.Name",
    Inn:"$Summary.Organization.Inn",
    Ogrn:"$Summary.Organization.Ogrn",
    Category:"$RegistryRecord.Category",
    Risks:"$Risks.Current.Category",
    Products:"$Requests.EmissionObject.Products",
    Air:{$cond: { if: { $eq: [ { $size:"$Summary.EmissionObject.Facts.Air.Facts" }, 0 ] } , then: '', else: "$Summary.EmissionObject.Facts.Air.Facts" } },
    Water:{$cond: { if: { $eq: [ { $size:"$Summary.EmissionObject.Facts.Water.Facts" }, 0 ] } , then: '', else: "$Summary.EmissionObject.Facts.Water.Facts" } },
    Waste:"$Summary.EmissionObject.Facts.Waste.TotalAnnualValue", 
    CategoryCriteria:"$RegistryRecord.Criteria"
}},
{ $project: {
    RegistryRecord:"$RegistryRecord",
    EmissionObject:"$EmissionObject",
    Organization:"$Organization",
    Inn:"$Inn",
    Ogrn:"$Ogrn",
    Category:"$Category",
    Risks:"$Risks",
    Products:"$Products",
    Air:"$Air",
    Water:"$Water",
    Waste:"$Waste",      
    CategoryCriteria:'$CategoryCriteria'
}},
{$unwind: "$Air"},
{ $project: {
    RegistryRecord:"$RegistryRecord",
    EmissionObject:"$EmissionObject",
    Organization:"$Organization",
    Inn:"$Inn",
    Ogrn:"$Ogrn",
    Category:"$Category",
    Risks:"$Risks",
    Products:"$Products",
    Air:{$concat:["$Air.Name"," - ",{ $toString: "$Air.AnnualValue"}]},
    Water:"$Water",
    Waste:"$Waste",   
    CategoryCriteria:'$CategoryCriteria'    
}},
{ $group: {
    _id:{
    RegistryRecord:"$RegistryRecord",
    EmissionObject:"$EmissionObject",
    Organization:"$Organization",
    Inn:"$Inn",
    Ogrn:"$Ogrn",
    Category:"$Category",
    Risks:"$Risks",
    Products:"$Products",
    Water:"$Water",
    Waste:"$Waste",
    CategoryCriteria:'$CategoryCriteria'
        },
    Air: {$push: {$concat: ["$Air"]}}}},
{$project:{
    _id:'$_id',
    Water:"$_id.Water",
    Air: {$reduce: {input: "$Air",initialValue: '', in: {$concat: ["$$value", ", ", "$$this"]}}}
            }
},
{$unwind: "$Water"},
{$project:{
    RegistryRecord:"$_id.RegistryRecord",
    EmissionObject:"$_id.EmissionObject",
    Organization:"$_id.Organization",
    Inn:"$_id.Inn",
    Ogrn:"$_id.Ogrn",
    Category:"$_id.Category",
    Risks:"$_id.Risks",
    Products:"$_id.Products",
    Air:'$Air',
    Waste:"$_id.Waste",
    CategoryCriteria:'$_id.CategoryCriteria',
    Water:{$concat:["$Water.Name"," - ",{ $toString: "$Water.AnnualValue"}]},
    
}},
 {$group: {
    _id:{
    RegistryRecord:"$RegistryRecord",
    EmissionObject:"$EmissionObject",
    Organization:"$Organization",
    Inn:"$Inn",
    Ogrn:"$Ogrn",
    Category:"$Category",
    Risks:"$Risks",
    Products:"$Products",
    Air:'$Air',
    Waste:"$Waste",
    CategoryCriteria:'$CategoryCriteria'},
    Water: {$push: {$concat: ["$Water"]}}}},
{$project:{
    _id:'$_id',
    Water: {$reduce: {input: "$Water",initialValue: '', in: {$concat: ["$$value", ", ", "$$this"]}}}
            }
},
{$project:{
    RegistryRecord:"$_id.RegistryRecord",
    EmissionObject:"$_id.EmissionObject",
    Organization:"$_id.Organization",
    Inn:"$_id.Inn",
    Ogrn:"$_id.Ogrn",
    Category:"$_id.Category",
    Risks:"$_id.Risks",
    Products:"$_id.Products",
    Air:'$_id.Air',
    Water:'$Water',
    Waste:"$_id.Waste",
    CategoryCriteria:'$_id.CategoryCriteria'
}},
{$unwind: "$Products"},
{$project:{
    RegistryRecord:"$RegistryRecord",
    EmissionObject:"$EmissionObject",
    Organization:"$Organization",
    Inn:"$Inn",
    Ogrn:"$Ogrn",
    Category:"$Category",
    Risks:"$Risks",
    Air:'$Air',
    Water:'$Water',
    Waste:"$Waste",
    CategoryCriteria:'$CategoryCriteria',
    Products:{$concat:["$Products.OkpdName","(Проектная мощность - ",{ $toString: "$Products.ProjectedValue"},", Объем - ",{ $toString: "$Products.Value"}," ", "$Products.MeasureName",")"]},
}},
{$group: {
    _id:{
    RegistryRecord:"$RegistryRecord",
    EmissionObject:"$EmissionObject",
    Organization:"$Organization",
    Inn:"$Inn",
    Ogrn:"$Ogrn",
    Category:"$Category",
    Risks:"$Risks",
    Air:'$Air',
    Water:'$Water',
    Waste:"$Waste",
    CategoryCriteria:'$CategoryCriteria'},
    Products: {$push: {$concat: ["$Products"]}}}},
{$project:{
    _id:'$_id',
    Products: {$reduce: {input: "$Products",initialValue: '', in: {$concat: ["$$value", ", ", "$$this"]}}}
            }
},
{$project:{
    RegistryRecord:"$_id.RegistryRecord",
    EmissionObject:"$_id.EmissionObject",
    Organization:"$_id.Organization",
    Inn:"$_id.Inn",
    Ogrn:"$_id.Ogrn",
    Category:"$_id.Category",
    Risks:"$_id.Risks",
    Products:"$Products",
    Air:'$_id.Air',
    Water:'$_id.Water',
    Waste:"$_id.Waste",
    Criterias:{$cond: { if: { $eq: [ { $size:"$_id.CategoryCriteria" }, 0 ] } , then: '', else: "$_id.CategoryCriteria" } },
}},
{$unwind: "$Criterias"},
{$lookup: {
       from: 'ZZZ_Criterias',
       localField: "Criterias",
       foreignField: "code",
       as: "Criterias"
}},
{$project:{
    RegistryRecord:"$_id.RegistryRecord",
    EmissionObject:"$_id.EmissionObject",
    Organization:"$_id.Organization",
    Inn:"$_id.Inn",
    Ogrn:"$_id.Ogrn",
    Category:"$_id.Category",
    Risks:"$_id.Risks",
    Products:"$Products",
    Air:'$_id.Air',
    Water:'$_id.Water',
    Waste:"$_id.Waste",
    Criterias:{$cond: { if: { $eq: [ { $size:"$Criterias" }, 0 ] } , then: '', else: "$Criterias" } },
}},
{$unwind: "$Criterias"},
{$project:{
    RegistryRecord:"$_id.RegistryRecord",
    EmissionObject:"$_id.EmissionObject",
    Organization:"$_id.Organization",
    Inn:"$_id.Inn",
    Ogrn:"$_id.Ogrn",
    Category:"$_id.Category",
    Risks:"$_id.Risks",
    Products:"$Products",
    Air:'$_id.Air',
    Water:'$_id.Water',
    Waste:"$_id.Waste",
    CategoryCriteria:"$Criterias.desna",
}},
{$project:{
    RegistryRecord:"$RegistryRecord",
    EmissionObject:"$EmissionObject",
    Organization:"$Organization",
    Inn:"$Inn",
    Ogrn:"$Ogrn",
    Category:"$Category",
    Risks:"$Risks",
    Products:"$Products",
    Air:'$Air',
    Water:'$Water',
    Waste:"$Waste",
    CategoryCriteria:"$CategoryCriteria",
}},
]})

When executing the export command, I get the following text

C:\Program Files\MongoDB\Tools\100\bin>mongoexport --host 10.1.101.111 --port 27017 --username fcaorpn --authenticationDatabase admin --collection tests --db Nero --out C:\Users\User\test.csv
Enter password:

2020-11-27T14:15:19.971+0300    connected to: mongodb://10.1.101.111:27017/
2020-11-27T14:15:21.021+0300    Nero.tests  0
2020-11-27T14:15:22.021+0300    Nero.tests  0
2020-11-27T14:15:23.021+0300    Nero.tests  0
2020-11-27T14:15:24.021+0300    Nero.tests  0
2020-11-27T14:15:25.021+0300    Nero.tests  0
2020-11-27T14:15:26.020+0300    Nero.tests  0
2020-11-27T14:15:27.020+0300    Nero.tests  0
2020-11-27T14:15:28.021+0300    Nero.tests  0
2020-11-27T14:15:29.021+0300    Nero.tests  0
2020-11-27T14:15:30.021+0300    Nero.tests  0
2020-11-27T14:15:31.023+0300    Nero.tests  0
2020-11-27T14:15:32.021+0300    Nero.tests  0
2020-11-27T14:15:33.021+0300    Nero.tests  0
2020-11-27T14:15:34.020+0300    Nero.tests  0
2020-11-27T14:15:35.020+0300    Nero.tests  0
2020-11-27T14:15:36.021+0300    Nero.tests  0
2020-11-27T14:15:37.020+0300    Nero.tests  0
2020-11-27T14:15:38.021+0300    Nero.tests  0
2020-11-27T14:15:39.021+0300    Nero.tests  0
2020-11-27T14:15:39.091+0300    Nero.tests  0
2020-11-27T14:15:39.091+0300    Failed: (Location16945) Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in.

Mongoexport does not have the allowDiskUse function, so as I understand it, I need to add it to the query that forms the collection. But I could not find a place where it can be inserted and do not get an error that this command is not used together with runCommand, only aggregate.

Where do I do that in the script and is there any problem doing this?

0

There are 0 answers