Collection creation and removal after restored from full logical backup - Mongodb

Hi

Before full logical backup database status

admin 13.34 MiB
config 3.61 MiB
doublecollection 94.25 MiB
pitr 95.52 MiB
restore 259.36 MiB
restore-test-1 94.63 MiB
singlecollection 40.00 KiB
test1db 306.50 MiB
withoutcollections 40.00 KiB

after Full Logical backup I created collection1 db and added ducuments to the collection1 under collection1 db

[direct: mongos] collection1> show collections
collection1

collection1 size:

sharded: false,
size: 426,
count: 9,
numOrphanDocs: 0,
storageSize: 36864,
totalIndexSize: 36864,
totalSize: 73728,
indexSizes: { id: 36864 },
avgObjSize: 47,
ns: ‘collection1.collection1’,
nindexes: 1,
scaleFactor: 1

database status

[direct: mongos] test> show dbs
admin 13.34 MiB
collection1 72.00 KiB
config 3.61 MiB
doublecollection 94.25 MiB
pitr 95.52 MiB
restore 259.36 MiB
restore-test-1 94.63 MiB
singlecollection 40.00 KiB
test1db 306.50 MiB
withoutcollections 40.00 KiB

After restored from full logical backup database status

[direct: mongos] test> show dbs
admin 13.34 MiB
collection1 72.00 KiB
config 3.61 MiB
doublecollection 94.25 MiB
pitr 95.52 MiB
restore 259.36 MiB
restore-test-1 94.63 MiB
singlecollection 40.00 KiB
test1db 306.50 MiB
withoutcollections 40.00 KiB

[direct: mongos] test> use collection1
switched to db collection1
[direct: mongos] collection1> show collections

[direct: mongos] collection1>

I could see collection1 db size is 72.00 KiB even I couldn’t see any collections under collection1 and this is the expected behaviour of logical backup.

If I know the collection name created under collection1 db, I will bring back the removed collection again using the command - db.createCollection(“collection1”)

and if I don’t want, I will delete the collection permanently the same way using the command db.dropCollection() or db.dropDatabase() after bring back the removed collection.

what if I don’t know the collection name, how I can bring back the removed collection and how I can delete the removed collection permanently ?

@gowtham-ji

what if I don’t know the collection name, how I can bring back the removed collection and how I can delete the removed collection permanently ?

Sorry I didn’t get what you exactly meant by deleting the removed collection. If you dropping a collection " db.collection.drop()" it will be a permanent delete of the schema/indexes. There is no need to do any further action. Can you elaborate the context ?

Additionally, if you removed the collection the only way to restore it from the backup. Well you have the feasibility to just restore the schema/index (“*.metadata.json”) without any data.

E.g, restoring only schema.

rsync --recursive --include="*.json" --exclude='*.bson' /backups/ /backups2/

ls -lh /backups2/test
total 4.0K
-rw-r--r--. 1 root root 324 Jan  1 14:23 test1.metadata.json

mongorestore -uroot -proot --db test /backups2/test/

Moreover, you can also track the changes (Create/Delete) in the oplog.rs collection which can be used for the PITR purpose.

rs0 [direct: primary] local> **db.oplog.rs.find(**{
...     "o.drop": "test1"
... }).sort({$natural: -1})
[
  {
    op: 'c',
    ns: 'test.$cmd',
    ui: UUID('b272134f-aec3-4575-a1e7-9efdeba111b4'),
    o: { drop: 'test1' },
    o2: { numRecords: 0 },
    ts: Timestamp({ t: 1736768019, i: 1 }),
    t: Long('1'),
    v: Long('2'),
    wall: ISODate('2025-01-13T11:33:39.909Z')
  }
]

Since its a capped collection so better to have a regular backup policy of this collection “oplog.rs” for the Recovery purpose.

For restorations --oplogReplay can be used with the mongorestore command.

  • List item

Let me know if this is what you looking for ?

Hi @anil.joshi

For the collection1 data base - collection1 72.00 KiB I could see still 72.00 KiB space occupied by collection1 72.00 KiB database even there is no data or collection under the collection database. How can I remove those 72.00 KiB space completely?