Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB extra Collections

I have a db called index having only one collection named student.

When I fire query db.students.find({}).count()

It shows 1000000 docs in it.

But when I used db.stats() It shows result like:-

{
        "db" : "index",
        "collections" : 3,
        "objects" : 1000004,
        "avgObjSize" : 59.95997216011136,
        "dataSize" : 59960212,
        "storageSize" : 87420928,
        "numExtents" : 14,
        "indexes" : 1,
        "indexSize" : 32458720,
        "fileSize" : 520093696,
        "nsSizeMB" : 16,
        "ok" : 1
}
  1. 3 collections how ?

  2. No of object 1000004 which is 4 extra from expected ?

And finally i did db.getCollectionNames() it shows [ "student", "system.indexes" ]

What is system.indexes ?

Please anybody elaborate on it?

I am new to the world of mongo.

like image 221
squiroid Avatar asked Apr 10 '15 07:04

squiroid


People also ask

Can a MongoDB have multiple collections?

As mentioned above, a single database can have multiple collections. The following creates multiple collections. Use the show collections commands to list all the collections in a database.

How many collections can you have MongoDB?

In general, we recommend limiting collections to 10,000 per replica set. When users begin exceeding 10,000 collections, they typically see decreases in performance. To avoid this anti-pattern, examine your database and remove unnecessary collections.

Can MongoDB handle millions of records?

Working with MongoDB and ElasticSearch is an accurate decision to process millions of records in real-time. These structures and concepts could be applied to larger datasets and will work extremely well too.


1 Answers

The mysterious 2 collections

There are two collections created when a user stores data in a database for the first time or a database is created explicitly.

The first one, system.indexes holds the information about the indices defined in the various collections of the database. You can even access it using

db.system.indexes.find()

The hidden one, system.namespaces holds some metadata about the database, actually the name of all existing entities from the point of view of the database management.

Although it is not shown, you can still access it:

db.system.namespaces.find()

Warning: Don't fiddle with either of them. Your database may well become unusable. You have been warned!

There can be even more than those two. Read System Collections in the MongoDB docs for details.

The mysterious 4 objects

Actually, If you have tried to access the system databases as shown above, this one becomes very easy. In a database called foobardb with a collection foo and the default index on _id, querying system.indexes will give a result like this (prettified):

{
  "v" : 1,
  "key" : {
    "_id" : 1
  },
  "name" : "_id_",
  "ns" : "foobardb.foo"
}

Note that this is a single document. The prettified output of the second query looks like this:

{ "name" : "foobardb.foo" }
{ "name" : "foobardb.system.indexes" }
{ "name" : "foobardb.foo.$_id_" }

Here, we have three documents. So we have 4 additional documents inside the metadata.

like image 77
Markus W Mahlberg Avatar answered Sep 28 '22 14:09

Markus W Mahlberg