MongoDB is a source-available, cross-platform, document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. (Wikipedia)
MongoDB is not an SQL-based database. In MongoDB, tables are referred to as Collections
, and Records
are referred to as Documents
.
Below, we discuss some useful MongoDB shell commands.
show dbs
use [name of database]
use users
db
db.createCollection("[name of collection]")
db.createCollection("customer")
show collections
db.[name of collection].insert({})
db.customer.insert({"name": "Theodore", "gender": "M"})
Or, put the documents in an array to insert more than one document:
db.customer.insert([
{"name": "Theodore", "gender": "M"},
{"name": "Jane Doe", "gender": "F"},
{"name", "John Doe", "gender": "M"}
])
db.[collection Name].find()
or
db.[collection name].find().pretty()
for a well indented list.db.customer.find().pretty()
db.customer.find({},{id:1}).pretty()
The above will only display _id
the result.
$set
db.[name of collection].update({},{$set: {}})
db.customer.update(
{"name":"Theodore"},
{$set:
{"name": "Theodore Kelechukwu Onyejiaku"}
}
)
$rename
db.customer.update(
{"name": "Theodore Kelechukwu Onyejiaku"},
{$rename:
{"gender":"sex"}
}
)
$unset
db.customer.update(
{"name": "Theodore Kelechukwu Onyejiaku"},
{$unset:
{"sex": 1}
}
)
db.[name of collections].remove({})
or
db.[name of collections].remove({}, {justOne: true})
to remove just one with the unique match.db.customer.remove({"name":"Jane Doe"})
We have gone over some of the basic and most frequently used MongoDB commands. Feel free to try them out, and of course, find some more advanced commands. Happy shelling!