Access mongo CLI
$> mongo
Show databases
show dbs;
Show Collections
show collections;
Filter Unique Values
Filter unique values (only the values) from a column (first you must «use db», only change «collection» and «column» do not change the db prefix)
> db['collection'].distinct('COLUMN')
Filter Unique Values on duplicated elements and show all the fields of the document
I had to filter an specific field, for example:
computer:1, mac: aa:bb:cc:dd:ee computer:2, mac: a1:b1:c1:d1:e1 computer:1, mac: aa:bb:cc:dd:ee computer:2, mac: a1:b1:c1:d1:e1 computer3, mac: a2:b2:c2:d2:e2
(Yes, the same data is inserted over and over again with certain variations on other fields, but each MACAddress is unique).
And I would like to return:
computer:1, mac: aa:bb:cc:dd:ee computer:2, mac: a1:b1:c1:d1:e1 computer3, mac: a2:b2:c2:d2:e2
So,
Filter the unique values using .unique()
- do a (for) loop over that values
def displayInventoryNotebooksByMAC(): """ Este metodo lista toda la informacion de las notebooks filtrando por unica MAC Address posteriormente agrega los datos en una nueva coleccion para inventariado """ uniqueNotebook=[] notebooksByMAC = displayTotalNotebooksByMAC() # En esta primera etapa, mostraremos notebooks unicas por MAC for mac in notebooksByMAC: uniqueNotebooks = collection.find_one( {'MACADD': mac } ) uniqueNotebook.append(uniqueNotebooks) # Ahora, agregamos esos resultados a la nueva coleccion que servira de inventario # Buscamos la MAC antes de agregar pues no debemos sumar duplicados if invCollection.find( { 'MACADD':mac } ).count() == 0 : invCollection.insert(uniqueNotebooks) return uniqueNotebook
Remove fields from all documents
db.example.update({}, {$unset: {words:1}}, false, true);
Backup mongodb
This is the Recommended method by mongo
mortiz@tinylab:/var/$ mongodump --host localhost:27017
If you receive this error, be sure you are in a path with write access:
2019-06-03T10:59:27.532-0300 Failed: error dumping metadata: error creating directory for metadata file dump/myProject: mkdir dump: permission denied
So a dump folder will be created with all the binary data of your mongoDB:
drwxr-xr-x 4 mortiz mortiz 4096 Jun 3 10:59 dump mortiz@tinylab:~$ cd dump/ mortiz@tinylab:~/dump$ ls -ltr total 8 drwxr-xr-x 2 mortiz mortiz 4096 Jun 3 10:59 myProject drwxr-xr-x 2 mortiz mortiz 4096 Jun 3 10:59 admin mortiz@tinylab:~/dump$ cd myProject/ mortiz@tinylab:~/dump/myProject$ ls -ltr total 900 -rw-r--r-- 1 mortiz mortiz 138 Jun 3 10:59 myProjectLaptops.metadata.json -rw-r--r-- 1 mortiz mortiz 133 Jun 3 10:59 Desktops.metadata.json -rw-r--r-- 1 mortiz mortiz 514625 Jun 3 10:59 myProjectLaptops.bson -rw-r--r-- 1 mortiz mortiz 395518 Jun 3 10:59 Desktops.bson mortiz@tinylab:~/dump/myProject$ cat Desktops.bson
Restore mongodb
After using mongodump you can restore easily by using mongorestore and the directory name where is your backup, here «dump» is the default name of the directory after executing mongo restore:
root@myserer:~# mongorestore dump 2019-06-03T10:37:11.057-0400 building a list of dbs and collections to restore from dump dir 2019-06-03T10:37:11.058-0400 reading metadata for myproject.myprojectlaptops from dump/myproject/myprojectlaptops.metadata.json 2019-06-03T10:37:11.089-0400 restoring myproject.myprojectlaptops from dump/myproject/myprojectlaptops.bson 2019-06-03T10:37:11.090-0400 reading metadata for myproject.Desktops from dump/myproject/Desktops.metadata.json 2019-06-03T10:37:11.110-0400 restoring myproject.Desktops from dump/myproject/Desktops.bson 2019-06-03T10:37:11.119-0400 restoring indexes for collection myproject.myprojectlaptops from metadata 2019-06-03T10:37:11.119-0400 finished restoring myproject.myprojectlaptops (1042 documents) 2019-06-03T10:37:11.126-0400 restoring indexes for collection myproject.Desktops from metadata 2019-06-03T10:37:11.127-0400 finished restoring myproject.Desktops (1213 documents) 2019-06-03T10:37:11.127-0400 done
Delete documents with empty fields
This will delete ALL documents with that empty/null field:
db.collection.deleteMany( { "FIELD" : null } )
But sometimes isn’t null just «empty», so:
db.collection.deleteMany( { "FIELD" : "" } )
Trim specific field in all documents of a collection
> db.collection.update( {}, [{ $set: { YOURFIELD: { $trim: { input: "$YOURFIELD" } } } }], { multi: true } ) WriteResult({ "nMatched" : 32031, "nUpserted" : 0, "nModified" : 9691 })