[SERVER-32207] shell does not report a duplicate key error when creating a unique index fails Created: 07/Dec/17  Updated: 08/Jan/24  Resolved: 17/Aug/18

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Derek Scherger Assignee: Mira Carey
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

$ cat tmp/foo.js
db.foo.drop();
db.foo.insert({ foo: 'bar' });
db.foo.insert({ foo: 'bar' });
db.foo.createIndex({ foo: 1 }, { unique: true });
db.foo.getIndexes();
 
$ mongo tmp/foo.js
MongoDB shell version: 3.2.18
connecting to: test
 
$ echo $?
0
 
$ mongo < tmp/foo.js
MongoDB shell version: 3.2.18
connecting to: test
true
WriteResult({ "nInserted" : 1 })
WriteResult({ "nInserted" : 1 })
{
	"ok" : 0,
	"errmsg" : "E11000 duplicate key error index: test.foo.$foo_1 dup key: { : \"bar\" }",
	"code" : 11000
}
[
	{
		"v" : 1,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "test.foo"
	}
]
bye
 
$ echo $?
0

Participants:

 Description   

running a scripted set of commands from the shell with `mongo tmp/foo.js` does not report errors



 Comments   
Comment by Mira Carey [ 17/Aug/18 ]

Unfortunately the shell doesn't offer anything like bash "set -e" and we're not in a great position to plumb one at this point.

The shell's error return as a process is largely related to errors in it's own processing, returning non-zero for errors in connect or for syntax errors.

For errors from mongod, its not obvious when to turn those into top level errors. A user running a variety of commands may or may not expect any particular command result to succeed with any particular code. Executing an insert and seeing a duplicate key error can be perfectly valid, for instance.

If you'd like to verify that any particular statement succeeded, you may assert that any particular statement succeeded with commands like:

assert.writeOK(db.test.insert({a:1}));

or

assert.commandWorked(db.test.runCommand({});

Comment by Derek Scherger [ 09/Dec/17 ]

What I'm looking for is a generic, reliable way to determine whether an arbitrary script encountered errors or not.
Writing errors to stderr would be a good start. Exiting with a non-zero return code after the first error would be ideal. Neither of these things seem to be happening.

You are correct in that I can change the script to use `printjson(db.foo.createIndex(

{ foo: 1 }

,

{ unique: true }

));` which does display the error in a human readable way but if the script is running in a non-interactive environment that doesn't really help all that much. Additionally I would need to change every statement in the script to use printjson which is far from ideal.

Comment by Mark Agarunov [ 08/Dec/17 ]

Hello derek@echologic.com,

Thank you for the report. When using the mongo shell with a script, output can be returned by using print() or printjson(). This includes errors that are returned from mongod.

Thanks,
Mark

Generated at Thu Feb 08 04:29:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.