MongoDB BSON Restore, Converting to JSON, and More MongoDB Helpful Commands

Wednesday, May 25th, 2022

MongoDB Helpful Scripts & Commands

Restoring BSON Backups

The below batch script helps you extract all .gz zipped BSON MongoDB table backup files and then restore these tables to a particular Mongo database easily:

@ECHO ON
SET SourceDir=%~dp0
cd %SourceDir%
mkdir "extracted"
mkdir "extracted\json"
FOR /R %SourceDir% %%A IN ("*.gz") DO "C:\Program Files\7-Zip\7z.exe" x "%%~A" -o"%SourceDir%\extracted"
mongorestore -d {DATABASE_NAME_TO_RESTORE_TABLES_INTO} --host localhost:27017 "extracted"

Converting MongoDB Tables and Data to Proper JSON

If you want to convert MongoDB tables and their data into JSON, you can use the below commands:

mongoexport -d {DATABASE_TO_EXPORT_FROM} --host localhost:27017 -c {TABLE_NAME_TO_EXPORT_CONVERT_INTO_JSON} --jsonArray --pretty -o "%SourceDir%\extracted\json\{TABLE_NAME_BEING_CONVERTED_TO_JSON_NAME}.json"

With older versions of MongoDB, the json file export doesn't actually contain valid JSON. To fix the $date and $numberLong properties which are invalid according to JSON specifications, you can run the below Python 2.7 script:

######################################
# About                              #
######################################

# Author:   Eric Arnol-Martin https://eamster.tk
# Purpose:  Replaces Mongo's Exported DateTime Format with Proper DateTime String Representations for Easier Import for Other Databases / Programming Languages
# Expects:  Mongo JSON Exported Pretty File.  
#			For example, a file produced by a command similar to 
#			"mongoexport -d {db_name} --host localhost:27017 -c {table_name} --jsonArray --pretty -o {table_name}.json"
# Tests:    RegEx Test Link:  https://regexr.com/68l7r
# Outputs:  Creates a copy of the input JSON file with DateTime objects replaced with their proper string representation in the same directory as the original file 
#			with the same file name suffixed with "_NEW" at the end of it.
# Sources:  https://stackoverflow.com/questions/2503413/regular-expression-to-stop-at-first-match
#			https://stackoverflow.com/questions/159118/how-do-i-match-any-character-across-multiple-lines-in-a-regular-expression

######################################
# Imports                            #
######################################

import re
from os.path import exists
import fileinput


######################################
# Actual Program                     #
######################################

path = input ("Enter path or name of file to parse: ")
prevPiece = None
content_new = ""
boolReplaceFromNextLine = False
boolHandlingLong = False

if exists(path):
	# Clear new file
	b = open(path + "_NEW", "w+")
	b.close()
	count = 0
	recordCount = 0

	for line in fileinput.input(files=path):
		count = count + 1
		if '"$date":' in line: 
			content_new = content_new[0:content_new.rindex('{')] + line.replace('"$date": {', '').replace('"$date":', '').replace('\n', '').strip();
			boolReplaceFromNextLine = True
		else:
			if boolReplaceFromNextLine:
				if '"$numberLong":' in line:
					content_new = content_new + line.replace('"$numberLong":', '').replace('\n', '').strip();
					boolReplaceFromNextLine = True
					boolHandlingLong = True
				else:
					if boolHandlingLong:
						content_new = content_new + line.replace('}', '').strip();
						boolReplaceFromNextLine = True
						boolHandlingLong = False
					else:
						boolReplaceFromNextLine = False
						content_new = content_new + line.replace('}', '').strip() + '\n';
			else:
				content_new = content_new + line		
		
		if line == '},\n' and content_new:
			recordCount = recordCount + 1
			b = open(path + "_NEW", "a+")
			b.write(content_new)
			b.close()	
			content_new = ""
			print('Record ' + str(recordCount) + ' processed... adding it to the file...')
			
		prevPiece = line
	
	if content_new:
		b = open(path + "_NEW", "a+")
		b.write(content_new)
		b.close()	
		content_new = ""
		recordCount = recordCount + 1
		print('Record ' + str(recordCount) + ' processed... adding it to the file...')

WinRar Create SFX Self Extracting Installation Wizard and Add Registry Settings

Saturday, January 19th, 2013

Creating a SFX Installation Wizard with WinRar that Can Modify Registry Settings

WinRar's "SFX options…" dialog does not contain any information or guides that help you modify registry settings after the files for your installation have been extracted.  This is a hinderance, but it is extremely possible to modify registry settings by running a batch file after the installation has been completed.  First, make sure the .bat file is included with the rest of your installation files that you selected before making the SFX archive.  In the "Advanced SFX options" window, click on the Setup tab.  Under "Run after extraction", type in just the name of the .bat file you're going to be running.

The Batch File Itself:

This template batch file checks to see if the operating system is 32-bit or 64-bit.  This is an important distincting as it will affect how you setup keys for each type of operating system.  64-bit system keys are usually placed in the Wow6432Node key, so it is important to make this distinction so that your registry settings will work properly.  This batch file called "Program Registry Adder 1.0" provides examples on how to add and modify registry keys depending on the operating system type.

Download Program Registry Adder 1.0

Adding Keys with a .bat (batch) File:

To add specific keys, you use the following parameters:

REG ADD "HKLM\SOFTWARE\My Proggy\" /f /v "InstallDir" /t REG_SZ /d "%PPROG%

The first parameter specifies which hive and key should be created.  The /f flag forces the addition.  The /v flag is the name of the key that should be created.  The /t flag specifies what type of Registry key should be created.  The example uses REG_SZ, which is just a string.  The possible types are:

Value Type (Use this in the batch File)
Binary REG_BINARY
DWORD REG_DWORD
Expandable String REG_EXPAND_SZ
Multi-String REG_MULTI_SZ
String REG_SZ
Binary REG_RESOURCE_LIST
Binary REG_RESOURCE_REQUIREMENTS_LIST
Binary REG_FULL_RESOURCE_DESCRIPTOR
None REG_NONE
Link REG_LINK
QWORD REG_QWORD

 

Check out more information here.

The /d flag contains the value of the key.

Conclusion

Using both a self extracting installation wizard and a batch file for registry key settings makes for the perfect free to use professional looking installation script. You can always run the batch file silently by removing all pause statements in the script. 

Keep Num Lock On at Startup on Windows Computers

Thursday, March 29th, 2012

Windows XP, Vista, and Windows 7 :: Keep Num Lock On at Startup

Despite what Microsoft claims, I've seen some computers not revert back to their last set num lock setting when Windows was last shut down.  Granted, this could be caused by Enterprise configurations.Here's a simple batch file that should keep Num Lock always ON when a user logs in.  It may or may not need to be run with administrator privileges based on your enterprise configuration.  I've had several users complain about the fact that Num Lock is turned off when they turn on their computers in the morning.  As a result, this batch file allows you to enable the setting that keeps num lock enabled after a user logs in with minimal effort.  I'm still under the impression that this is more work than the user simply pressing the num lock key to turn it back on.

REGEDIT4
@ECHO OFF
CLS
REGEDIT.EXE /S "%~f0"
EXIT

[HKEY_CURRENT_USER\Control Panel\Keyboard\]
"InitialKeyboardIndicators"="2"