MongoDB — Little code for beginners

Natan Cabral
2 min readMay 2, 2021

MongoDB is a document database built on a scale-out architecture that has become popular with developers of all kinds who are building scalable applications using agile methodologies. NoSQL and BSON format (like JSON files).

Install your MongoDB. Download or Terminal (Debian)

Unix package

$ sudo apt-get install mongodb
$ mongo
$ show dbs // to list databases

Create a Database

$ use databasename // create a new or change to database (var db)
$ db.createUser({}) // or use databasename.createUser({}); // user access:

use databasename.createUser({
user: "username",
pwd: passwordPrompt(), // Or "yourpassword"
roles: [ "readWrite", "dbAdmin" ]
})

Read document

Create Collection

Now our database is called db:
$ db.createCollection(‘clients’);

Insert

Inside clients:
$ db.clients.insert({first_name:”Natan”, last_name:”Cabral”});
$ db.clients.find(); // show registers
$ db.clients.find().pretty(); // show registers

// {
// “_id” : ObjectId(“608ed7f17dd7aef6631af795”),
// “first_name” : “Natan”,
// “last_name” : “Cabral”
// }

Nicely!

Update

$set, $inc, $unset

// db.clients.update({where},{$type:{data}});
// db.clients.update({where},{fulldata});

  • update a data

$ db.clients.update({first_name: ’Natan’}, {$set: {age:30}});
// age 30
$ db.clients.update({first_name: ’Natan’}, {$inc: {age:5}});
// age: 35
$ db.clients.update({first_name: ’Natan’}, {$unset: {age:1}});
// age: 1/true/something
// undefined

  • Insert if not find

You can insert data when try update if not find register.

$ db.clients.update({first_name: ’Mary’}, {first_name: ’Mary’, last_name: ‘Cabral’ }, {upsert: true});

  • Fields

Rename property/field/column:

$ db.clients.update({first_name: ’Mary’}, {$rename:{“last_name”:”surname”});
$ db.clients.find().pretty(); // show registers

// {
// “_id” : ObjectId(“608ed7f17dd7aef6631af795”),
// “first_name” : “Natan”,
// “surname” : “Cabral”,
// }

You can rename all fields:
// all properties called last_name change to surname, no only one.
// 3 samples:

$ db.clients.updateMany({},{$rename:{“last_name”:”surbname”}});
$ db.clients.update({}, {$rename:{“olds”:”new”}}, false, true);
$ db…update({“old”: {$exists: true}}, {$rename:{“old”:”new”}}, false, true);
// false, true is { upsert: false, multi: true }

Fields inside fields
$ db.clients.update({first_name:’Natan’},{$set:{‘address.city’:’address’})

Remove

All
$ db.clients.remove({first_name: ’Natan’});
Just one
$ db.clients.remove({first_name: ’Natan’}, {justOne: true});

Find

Between
$ db.clients.find({ age : { $gt : 30, $lt : 60}});
Words
$ db.clients.find({ name: /Nat/ }); // case sensitive
$ db.clients.find({ name: /NaT/i }); // case insensitive query
Advanced
$ db.getCollection(‘clients’).distinct(“cities”).limit(2);
$ db.getCollection(‘clients’).find({},{“_id”:0, “name”:1, “last”:1}).limit(3);
// return only name and last fields, if removed _id, show _id field on all returns.

Sort

$ db.clients.sort({last_name: -1}).find(); // -1|1 / asc|desc

Index

$ db.clients.createIndex({ last_name: ‘text’ }, { default_language: ‘none’ })
// on my case pt, but set none
$ db.clients.find({ $text: { $search: ‘Cabral’ } });
// remove index
$db.clients.dropIndex(‘last_name’);

Pretty cool! ;)

1/2/

--

--

Natan Cabral

Full Stack Developer | Dev Java, Node.js, TypeScript, React.js, Vue.js, Express.js, Next.js, Rest API, Laravel, Databases, MongoDB, Unix distro and Open Source