The Basics

CRUD


Create

  • Create a collection named languages
  • Insert five documents into the collection (see code above)
# ------- Create/Connect to db
test> use training
switched to db training

# Create Collection "languages"
training> db.createCollection("languages")
{ ok: 1 }

# Insert five documents into the collection
# ----------------------------------------
# DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.

training> db.languages.insert({"name":"java","type":"object oriented"}) 
{
  acknowledged: true,
  insertedIds: { '0': ObjectId('6716c048e9fefa0310964033') }
}
training> db.languages.insert({"name":"python","type":"general purpose"})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId('6716c048e9fefa0310964034') }
}
training> db.languages.insert({"name":"scala","type":"functional"})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId('6716c048e9fefa0310964035') }
}
training> db.languages.insert({"name":"c","type":"procedural"})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId('6716c048e9fefa0310964036') }
}
training> db.languages.insert({"name":"c++","type":"object oriented"})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId('6716c082e9fefa0310964037') }
}

Read

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
training> db.languages.countDocuments()
5

# List ALL documents

training> db.languages.find()
[
  {
    _id: ObjectId('6716c048e9fefa0310964033'),
    name: 'java',
    type: 'object oriented'
  },
  {
    _id: ObjectId('6716c048e9fefa0310964034'),
    name: 'python',
    type: 'general purpose'
  },
  {
    _id: ObjectId('6716c048e9fefa0310964035'),
    name: 'scala',
    type: 'functional'
  },
  {
    _id: ObjectId('6716c048e9fefa0310964036'),
    name: 'c',
    type: 'procedural'
  },
  {
    _id: ObjectId('6716c082e9fefa0310964037'),
    name: 'c++',
    type: 'object oriented'
  }
]

# List FIRST document
training> db.languages.findOne()
{
  _id: ObjectId('6716c048e9fefa0310964033'),
  name: 'java',
  type: 'object oriented'
}

# List FIRST 3 documents
training> db.languages.find().limit(3)
[
  {
    _id: ObjectId('6716c048e9fefa0310964033'),
    name: 'java',
    type: 'object oriented'
  },
  {
    _id: ObjectId('6716c048e9fefa0310964034'),
    name: 'python',
    type: 'general purpose'
  },
  {
    _id: ObjectId('6716c048e9fefa0310964035'),
    name: 'scala',
    type: 'functional'
  }
]

# Query for the word "python"
training> db.languages.find({"name":"python"})
[
  {
    _id: ObjectId('6716c048e9fefa0310964034'),
    name: 'python',
    type: 'general purpose'
  }
]

# Query for the string "object oriented"
training> db.languages.find({"type":"object oriented"})
[
  {
    _id: ObjectId('6716c048e9fefa0310964033'),
    name: 'java',
    type: 'object oriented'
  },
  {
    _id: ObjectId('6716c082e9fefa0310964037'),
    name: 'c++',
    type: 'object oriented'
  }
]

# Filter to list ONLY the field= name
training> db.languages.find({},{"name":1})
[
  { _id: ObjectId('6716c048e9fefa0310964033'), name: 'java' },
  { _id: ObjectId('6716c048e9fefa0310964034'), name: 'python' },
  { _id: ObjectId('6716c048e9fefa0310964035'), name: 'scala' },
  { _id: ObjectId('6716c048e9fefa0310964036'), name: 'c' },
  { _id: ObjectId('6716c082e9fefa0310964037'), name: 'c++' }
]

# Filter to EXCLUDE one field= name
training> db.languages.find({},{"name":0})
[
  {
    _id: ObjectId('6716c048e9fefa0310964033'),
    type: 'object oriented'
  },
  {
    _id: ObjectId('6716c048e9fefa0310964034'),
    type: 'general purpose'
  },
  { _id: ObjectId('6716c048e9fefa0310964035'), type: 'functional' },
  { _id: ObjectId('6716c048e9fefa0310964036'), type: 'procedural' },
  {
    _id: ObjectId('6716c082e9fefa0310964037'),
    type: 'object oriented'
  }
]

# Double Filter for type=string and INCLUDE field=name only
training> db.languages.find({"type":"object oriented"},{"name":1})
[
  { _id: ObjectId('6716c048e9fefa0310964033'), name: 'java' },
  { _id: ObjectId('6716c082e9fefa0310964037'), name: 'c++' }
]

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
training> db.languages.updateMany({},{$set:{"description":"programming language"}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 5,
  modifiedCount: 5,
  upsertedCount: 0
}

# Set the creator value of any name=python collection
training> db.languages.updateMany({"name":"python"},{$set:{"creator":"Guido van Rossum"}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}

# Set compiled=true for ALL languages=object oriented
training> db.languages.updateMany({"type":"object oriented"},{$set:{"compiled":ttrue}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 2,
  modifiedCount: 2,
  upsertedCount: 0
}

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
training> db.languages.remove({"name":"scala"})
DeprecationWarning: Collection.remove() is deprecated. Use deleteOne, deleteMany, findOneAndDelete, or bulkWrite.
{ acknowledged: true, deletedCount: 1 }

# Count documents confirms the deletedCount: 1 above
training> db.languages.countDocuments()
4

# Delete the documents with type=object oriented
training> db.languages.remove({"type":"object oriented"})
{ acknowledged: true, deletedCount: 2 }

# So if we count again we should have 2 remaining
training> db.languages.countDocuments()
2

# Delete ALL documents
training> db.languages.remove({})
{ acknowledged: true, deletedCount: 2 }