~$ mongosh mongodb://root:hGjWJ1sdReI6q3ajJ3gXXuAy@172.21.217.115:27017
671677e0ac0fee924e964032
Current Mongosh Log ID: //<credentials>@172.21.217.115:27017/?directConnection=true&appName=mongosh+2.3.1
Connecting to: mongodb:3.6.3
Using MongoDB: 2.3.1
Using Mongosh:
//www.mongodb.com/docs/mongodb-shell/
For mongosh info see: https:
help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
To -out by running the disableTelemetry() command.
You can opt
------
The server generated these startup warnings when booting2024-10-21T14:45:50.755+0000:
2024-10-21T14:45:50.755+0000: ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2024-10-21T14:45:50.755+0000: ** See http://dochub.mongodb.org/core/prodnotes-filesystem
------
> db.version()
test3.6.3
# List all DBs
> show dbs
test80.00 KiB
admin 48.00 KiB
config 64.00 KiB
local
# Create or connect to DB training
> use training
test
switched to db training
# Create a collection in DB training
> db.createCollection("mycollection")
training1 }
{ ok:
# List all collections
> show collections
training mycollection
MongoDB Cloud Server
Start Server
- Click on Open MongoDB Page in IDE
- Create Instance
- Click MongoDB CLI
- Check version
Show All DBs
show dbs
Create New DB
- Create a new DB named training
use training
- If the DB already exists it will connect to it and start using it
Create Collection
- Create a collection named mycollection inside the training DB
db.createCollection("mycollection")
Show All Collections
show collections
Insert Document
- Let’s insert dome documents into the collection mycollection
db.mycollection.insert({"color":"white","example":"milk"})
- The above command inserts the json document {“color”:”white”,”example”:”milk”} into the collection.
- Let’s insert another one:
db.mycollection.insert({"color":"blue","example":"sky"})
- Insert more as you see below in the code
> db.mycollection.insert({"color":"white","example":"milk"})
training
# DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
acknowledged: true,'0': ObjectId('67167ac0ac0fee924e964033') }
insertedIds: {
}
# Let's insert another one
> db.mycollection.insert({"color":"blue","example":"sky"})
training
{
acknowledged: true,'0': ObjectId('67167b3eac0fee924e964034') }
insertedIds: {
}
# Let's insert three more
> db.mycollection.insert({"color":"red","example":"wall"})
training
{
acknowledged: true,'0': ObjectId('67167bc4ac0fee924e964035') }
insertedIds: {
}> db.mycollection.insert({"color":"orange","example":"sun"})
training
{
acknowledged: true,'0': ObjectId('67167bd7ac0fee924e964036') }
insertedIds: {
}> db.mycollection.insert({"color":"green","example":"money"})
training
{
acknowledged: true,'0': ObjectId('67167c15ac0fee924e964037') }
insertedIds: {
}
# Now let's count all the documents
> db.mycollection.countDocuments()
training5
# Let's list all the documents in the collection
> db.mycollection.find()
training
[
{'67167ac0ac0fee924e964033'),
_id: ObjectId('white',
color: 'milk'
example:
},
{'67167b3eac0fee924e964034'),
_id: ObjectId('blue',
color: 'sky'
example:
},
{'67167bc4ac0fee924e964035'),
_id: ObjectId('red',
color: 'wall'
example:
},
{'67167bd7ac0fee924e964036'),
_id: ObjectId('orange',
color: 'sun'
example:
},
{'67167c15ac0fee924e964037'),
_id: ObjectId('green',
color: 'money'
example:
}
# Disconnect from DB
> exit
training~:/~/project$
Count All Document
- Let’s count the number of documents in the collection
db.mycollection.countDocuments()
List All Documents
- You can list/show all the documents using find()
db.mycollection.find()
- Notice that MongoDB automatically add an Id field to every document
Exit
exit
- To disconnect from the DB