0

I have a simple collection posts with a few documents in it. I have set the profiling level to 2 allowing for profiling information to be collected into another collection system.profile. My goal is to use a mapReduce operation to collect information from the command entry grouped via various actors using this simple query:

db.system.profile.mapReduce(
    function()
    {
        emit( this.appName, this.command  );
    },
    function( key, values ) 
    {
        return JSON.stringify(values);
    },
    {
        query: {},
        out  : "command_maps"
    }
)

However, trying this in the REPL yields the following error

2021-05-10T22:09:33.494+0800 E QUERY    [js] Error: map reduce failed:{
    "ok" : 0,
    "errmsg" : "rename failed: { ok: 0.0, errmsg: \"error with source namespace: cannot write to 'test.tmp.mr.system.profile_22'\", code: 20, codeName: \"IllegalOperation\" }",
    "code" : 10076,
    "codeName" : "Location10076"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCollection.prototype.mapReduce@src/mongo/shell/collection.js:1145:1
@(shell):1:1

I suspect mongo is trying to temporarily write into the system.profile collection and fails due to it being readonly in a sense. Is there any way to execute this successfully ?

Unfortunately the documents are too large to add here but basically its sort of like this:

DOCUMENT

{ 
   "appName": "actor1",     
   "command": {"find": {},"ns" : "test.post""} 
}, 
{  "appName": "actor2", 
   "command": { "find": {},"ns" : "test.comments"} 
}
{
   "appName": "actor1",
   "command": { "insert": {},}
}

OUTPUT

{  appName: "actor1": command_maps: '[{command: "find"}, {command:"query"} ...]' } 
{    appName: "actor2": command_maps: '[{command: "find"}...]' } 

Version: MongoDB shell version v4.4.5

PS : mapReduce is working properly on regular collections

6
  • Why do you try to mapreduce the system.profile collection? Use an aggregation pipeline as recommended in documentation. May 11 at 12:31
  • Yeah, its quite an expensive operation but basically by doing aggregation we would end up with a cartesian product and we can't deterministically know the fields which the command subdocument will have. However in this situation its kind of weird that mongo is trying to rewrite into the same collection during the query.
    – harry_k
    May 12 at 5:55
  • Can you provide some sample data and desired result? I don't think an aggregation pipeline will cause problems. mapReduce does not seem to be "state of the art" anymore. May 12 at 6:30
  • main thing to note is the command document can vary in contents
    – harry_k
    May 12 at 14:18
  • Please put additional information into the question instead of a comment. May 12 at 14:24
0

So it seems special readWrite privileges are needed to execute mapReduce on system.profile collection. So I got the query to work eventually after checking through:

https://docs.mongodb.com/manual/tutorial/manage-users-and-roles/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.