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
Best Answer
So it seems special
readWrite
privileges are needed to execute mapReduce onsystem.profile
collection. So I got the query to work eventually after checking through:https://docs.mongodb.com/manual/tutorial/manage-users-and-roles/