Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does createdCollectionAutomatically mean?

Tags:

mongodb

When I create an index on a collection, one of the properties of the result document is createCollectionAutomatically:false.

db.myCollection.createIndex({"address":1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 3,
    "ok" : 1
}

What does it mean and when is this true?

like image 875
Gerald Mücke Avatar asked Dec 22 '25 19:12

Gerald Mücke


1 Answers

Found the answer here: https://docs.mongodb.com/manual/reference/command/createIndexes/#output

The createdCollectionAutomatically indicates if the operation created a collection. If a collection does not exist, MongoDB creates the collection as part of the indexing operation.

So when I run db.myCollection.createIndex({"address":1}) and myCollection does not exists, the result is

{
    "createdCollectionAutomatically" : true,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
like image 148
Gerald Mücke Avatar answered Dec 24 '25 08:12

Gerald Mücke