# ------- Create/Connect to db
> use training
test
switched to db training
# Create Collection "languages"
> db.createCollection("languages")
training1 }
{ ok:
# Insert five documents into the collection
# ----------------------------------------
# DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
> db.languages.insert({"name":"java","type":"object oriented"})
training
{
acknowledged: true,'0': ObjectId('6716c048e9fefa0310964033') }
insertedIds: {
}> db.languages.insert({"name":"python","type":"general purpose"})
training
{
acknowledged: true,'0': ObjectId('6716c048e9fefa0310964034') }
insertedIds: {
}> db.languages.insert({"name":"scala","type":"functional"})
training
{
acknowledged: true,'0': ObjectId('6716c048e9fefa0310964035') }
insertedIds: {
}> db.languages.insert({"name":"c","type":"procedural"})
training
{
acknowledged: true,'0': ObjectId('6716c048e9fefa0310964036') }
insertedIds: {
}> db.languages.insert({"name":"c++","type":"object oriented"})
training
{
acknowledged: true,'0': ObjectId('6716c082e9fefa0310964037') }
insertedIds: { }
Read
The Basics
- I’ll details the following operations using MongoDB on the Cloud for this section.
- Once you login to the account > Create a MongoDB instance > Open MongoDB CLI
- We already created a db named training in the other page MongoDB on the Cloud
CRUD
Create
- Create a collection named languages
- Insert five documents into the collection (see code above)
Count all the documents
db.languages.countDocuments()
List all the documents
db.languages.find()
List the first document
db.languages.findOne()
List the first 3 documents
db.languages.find().limit(3)
Query for the word “python”
db.languages.find({"name":"python"})
Query for string
db.languages.find({"type":"object oriented"})
Filter ONE field only
- Filter the output to include the field=name only
db.languages.find({},{"name":1})
Filter to EXCLUDE one field
- Filter the output to exclude the field=name
db.languages.find({},{"name":0})
Double Filter
- We want to filter by one field being equal to a string “type”:“object oriented” and
- Filter again to include only the field=name
db.languages.find({"type":"object oriented"},{"name":1})
# Count ALL documents
> db.languages.countDocuments()
training5
# List ALL documents
> db.languages.find()
training
[
{'6716c048e9fefa0310964033'),
_id: ObjectId('java',
name: type: 'object oriented'
},
{'6716c048e9fefa0310964034'),
_id: ObjectId('python',
name: type: 'general purpose'
},
{'6716c048e9fefa0310964035'),
_id: ObjectId('scala',
name: type: 'functional'
},
{'6716c048e9fefa0310964036'),
_id: ObjectId('c',
name: type: 'procedural'
},
{'6716c082e9fefa0310964037'),
_id: ObjectId('c++',
name: type: 'object oriented'
}
]
# List FIRST document
> db.languages.findOne()
training
{'6716c048e9fefa0310964033'),
_id: ObjectId('java',
name: type: 'object oriented'
}
# List FIRST 3 documents
> db.languages.find().limit(3)
training
[
{'6716c048e9fefa0310964033'),
_id: ObjectId('java',
name: type: 'object oriented'
},
{'6716c048e9fefa0310964034'),
_id: ObjectId('python',
name: type: 'general purpose'
},
{'6716c048e9fefa0310964035'),
_id: ObjectId('scala',
name: type: 'functional'
}
]
# Query for the word "python"
> db.languages.find({"name":"python"})
training
[
{'6716c048e9fefa0310964034'),
_id: ObjectId('python',
name: type: 'general purpose'
}
]
# Query for the string "object oriented"
> db.languages.find({"type":"object oriented"})
training
[
{'6716c048e9fefa0310964033'),
_id: ObjectId('java',
name: type: 'object oriented'
},
{'6716c082e9fefa0310964037'),
_id: ObjectId('c++',
name: type: 'object oriented'
}
]
# Filter to list ONLY the field= name
> db.languages.find({},{"name":1})
training
['6716c048e9fefa0310964033'), name: 'java' },
{ _id: ObjectId('6716c048e9fefa0310964034'), name: 'python' },
{ _id: ObjectId('6716c048e9fefa0310964035'), name: 'scala' },
{ _id: ObjectId('6716c048e9fefa0310964036'), name: 'c' },
{ _id: ObjectId('6716c082e9fefa0310964037'), name: 'c++' }
{ _id: ObjectId(
]
# Filter to EXCLUDE one field= name
> db.languages.find({},{"name":0})
training
[
{'6716c048e9fefa0310964033'),
_id: ObjectId(type: 'object oriented'
},
{'6716c048e9fefa0310964034'),
_id: ObjectId(type: 'general purpose'
},'6716c048e9fefa0310964035'), type: 'functional' },
{ _id: ObjectId('6716c048e9fefa0310964036'), type: 'procedural' },
{ _id: ObjectId(
{'6716c082e9fefa0310964037'),
_id: ObjectId(type: 'object oriented'
}
]
# Double Filter for type=string and INCLUDE field=name only
> db.languages.find({"type":"object oriented"},{"name":1})
training
['6716c048e9fefa0310964033'), name: 'java' },
{ _id: ObjectId('6716c082e9fefa0310964037'), name: 'c++' }
{ _id: ObjectId( ]
Update
Let’s update some documents based on a criteria
Add field to ALL documents
updateMany
is a command used to update documents- Syntax:
db.collection.updateMany({what documents to find},{$set:{what fields to set}})
- Let’s try it and add a field description with value programming language to ALL the documents
db.languages.updateMany({},{$set:{"description":"programming language"}})
Update One field ONLY
- Let’s set the creator value of the name=python collections
- So if any collection has name=python we will set the creator value
db.languages.updateMany({"name":"python"},{$set:{"creator":"Guido van Rossum"}})
- As you see from the confirmation only 1 document was updated
Update One Field for Many Collections
- Let’s set compiled=true for ALL languages=object oriented
db.languages.updateMany({"type":"object oriented"},{$set:{"compiled":true}})
- As you see from the confirmation, two records were updated
# Add a field description = programming language to ALL the documents
> db.languages.updateMany({},{$set:{"description":"programming language"}})
training
{
acknowledged: true,
insertedId: null,5,
matchedCount: 5,
modifiedCount: 0
upsertedCount:
}
# Set the creator value of any name=python collection
> db.languages.updateMany({"name":"python"},{$set:{"creator":"Guido van Rossum"}})
training
{
acknowledged: true,
insertedId: null,1,
matchedCount: 1,
modifiedCount: 0
upsertedCount:
}
# Set compiled=true for ALL languages=object oriented
> db.languages.updateMany({"type":"object oriented"},{$set:{"compiled":ttrue}})
training
{
acknowledged: true,
insertedId: null,2,
matchedCount: 2,
modifiedCount: 0
upsertedCount: }
Delete
Delete Entire document
- Let’s delete the document with name=scala
db.languages.remove({"name":"scala"})
- Delete the documents with type=object oriented
db.languages.remove({"type":"object oriented"})
- Delete all documents
db.languages.remove({})
# Delete all the documents with name=scala
> db.languages.remove({"name":"scala"})
trainingDeprecationWarning: Collection.remove() is deprecated. Use deleteOne, deleteMany, findOneAndDelete, or bulkWrite.
1 }
{ acknowledged: true, deletedCount:
# Count documents confirms the deletedCount: 1 above
> db.languages.countDocuments()
training4
# Delete the documents with type=object oriented
> db.languages.remove({"type":"object oriented"})
training2 }
{ acknowledged: true, deletedCount:
# So if we count again we should have 2 remaining
> db.languages.countDocuments()
training2
# Delete ALL documents
> db.languages.remove({})
training2 } { acknowledged: true, deletedCount: