[SERVER-12747] createIndexes: 'createdCollectionAutomatically' value is incorrect when collection creation fails Created: 14/Feb/14  Updated: 22/Feb/16  Resolved: 04/Feb/16

Status: Closed
Project: Core Server
Component/s: Index Maintenance
Affects Version/s: None
Fix Version/s: 3.3.2

Type: Bug Priority: Minor - P4
Reporter: Kamran K. Assignee: Max Hirschhorn
Resolution: Done Votes: 0
Labels: 26qa, neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-1627 add createIndexes command Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

var myDB = db.getSiblingDB('bugs');
var collName = new Array(120).join('z');
var coll = myDB[collName];
var res = coll.runCommand('createIndexes', {indexes: [{key: {'test': 1}, name: 'test'}]});
 
// no collection was actually created
assert.eq(0, myDB.getCollectionNames().length);
 
// but the result document reports true and this assert fails
assert.eq(res.createdCollectionAutomatically, false, tojson(res));

Sprint: Query 10 (02/22/16)
Participants:

 Description   

The createIndexes command will report that a collection has been created even if the creation of the new collection fails:

{
	"createdCollectionAutomatically" : true,
	"errmsg" : "exception: fully qualified namespace bugz.zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz is too long (max is 120 bytes)",
	"code" : 17381,
	"ok" : 0
}


Version: 7aad87285f8149e1979699d9767815740e98dff4



 Comments   
Comment by Githook User [ 04/Feb/16 ]

Author:

{u'username': u'visemet', u'name': u'Max Hirschhorn', u'email': u'max.hirschhorn@mongodb.com'}

Message: SERVER-12747 Add field to command result after collection is created.

It's possible for Database::createCollection() to throw an exception
after the "createdCollectionAutomatically" field has been added to the
BSONObjBuilder& result object in the "createIndexes" command.
Branch: master
https://github.com/mongodb/mongo/commit/66cd4ef95f4d5458678fcb772057e95b4ad5a26f

Comment by Max Hirschhorn [ 04/Feb/16 ]

This particular manifestation was fixed by a197796 under SERVER-12976, where the length of the collection name is checked as part of userAllowedWriteNS() prior to creating the collection and mutating the BSONObjBuilder& result.

It is possible to trigger this issue when running a config server and attempting to create a collection on a database other than admin, config, or local.

configsvr> db.version()
3.1.3
configsvr> db.getSiblingDB('test').mycoll.runCommand('createIndexes', {indexes: [{key: {'test': 1}, name: 'test'}]});
{
  "createdCollectionAutomatically" : true,
  "errmsg" : "can't create user databases on a --configsvr instance",
  "code" : 14037,
  "ok" : 0
}

However, after SERVER-17607, the contents of the BSONObjBuilder& result are no longer included in the response when an exception is thrown while executing a command. Thus, the issue with the createIndexes command mutating the BSONObjBuilder& result prior to creating the collection remains but isn't visible to the user.

configsvr> db.getSiblingDB('test').mycoll.runCommand('createIndexes', {indexes: [{key: {'test': 1}, name: 'test'}]});
{
  "ok" : 0,
  "errmsg" : "can't create user databases on a --configsvr instance",
  "code" : 14037
}

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