MongoDB
➢ Table----------Collection
➢ Row-----------documents
➢ Columns-----fields
➢ Database----Database
➢ BSON
➢ View all database: show dbs
➢ Db--- current database
➢ Create/switch database: use “database_name”
➢ Drop database: db.dropDatabase()
➢ View all collection: show collections
➢ Create a new collection: db.createCollection(“<collection_name>”)
➢ Delete collection: db.<collection_name>.drop()
➢ Inserting a document:
db.<collection_name>.insert(
{
“key”: “value”
}
)
Or
➢ Inserting many documents:
db.<collection_name>.insertMany(
[
{}
{}
]
)
Or, we can also use some JavaScript predefined function.
➢ View all documents:
db.<collection_name>.find();
Or
db.<collection_name>.find().pretty();
➢ Search for a document:
db.<collection_name>.find(
{
key: “value”
}
)
➢ Limiting the output:
db.<collection_name>.find().limit(<integer>)
➢ Counting the output:
db.<collection_name>.find().count()
Or ,
➢ Sorting the collections:
db.<collection_name>.find().sort({key:-1})
1:Ascending, -1:Descending
Or
➢ Updating the document:
db.<collection_name>.updateone(
{search},
{$set:{updated object}}
{upsert:boolean}
)
Upsert: Create the record if does not exist.
Upsert: Create the record if does not exist.
➢ Update operators:
$rename
***There are so many update operators***
➢ Delete document
db.<collection_name>.remove({search})