The Ghost Logo

MongoDB (from humongous) is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas.
JavaScript can be used in queries, aggregation functions (such as MapReduce), and sent directly to the database to be executed.

A list of snippets i find useful:



Find and convert to Date:

db.collection.find().forEach(function(doc) {
    doc.dateProp=new dateProp(doc.dateProp);     
    db.collection.save(doc);
 })

Find and convert to String:

db.collection.find().forEach(function(doc) {
    doc.tel=String(doc.tel);     
    db.collection.save(doc);
 })

Rename all colums from user to username:

db.collection.update({}, 
    {$rename:{"user":"username"}}, false, true);

Add a value to all fields:

db.collection.update({}, 
    {$set:{"username":"test"}}, false, true);

Backup to a folder:

mongodump -d myDb -o D:\Databases\Mongo\Backup\20feb2017

Restore from backup:

mongorestore -d myDb D:\Databases\Mongo\Backup\20feb2017\myDb

Import from CSV:

mongoimport --db myDb --collection myCollection --type csv --headerline --file C:\Test\file.csv

Variety.js : find MongoDB structure

variety hrbitech/myCollection --outputFormat='json'

Running MongoDB as a windows service:



1. Remove previos installed services:

mongod --remove

2. Install by selecting Log folder and DB Folder:

mongod --logpath C:\Databases\Mongo\Log\mongo.log --dbpath C:\Databases\Mongo --install

3. Start MongoDB service (next time it will run at windows start-up)

net start MongoDB