Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Created May 2, 2020 16:40
Show Gist options
  • Save sandrabosk/bb9613a6a11bc110e851f6b9b5edd9a6 to your computer and use it in GitHub Desktop.
Save sandrabosk/bb9613a6a11bc110e851f6b9b5edd9a6 to your computer and use it in GitHub Desktop.
# create database
> use characters
# add first document in the collection enemies (collection gets created now)
> db.enemies.insertOne({
    "name": "Blinky",
    "color": "Red"
})
 
# check if Blinky is added
> db.enemies.find().pretty()

# add multiple documents to the collection
> db.enemies.insertMany([ 
       {
        "name": "Inky",
        "color": "Cyan",
       },
       {
        "name": "Clyde",
        "color": "Orange"
       }])

# check if all documents are added (find all documents)
> db.enemies.find().pretty()

# find a specific document
> db.enemies.findOne({name: "Clyde"}).pretty()

# update one document
> db.enemies.updateOne({ name: "Inky" }, { $set: { color: "Red" } })

# update multiple documents
> db.enemies.updateMany({ color: "Red" }, { $set: { color: "Blue" } })

# delete one document from a collection
> db.enemies.deleteOne({name: "Clyde"})

# delete multiple documents from a collection
> db.enemies.deleteMany({color: "Blue"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment