Taula de continguts

Chapter 2 : The MongoDB Query Language + Atlas

CRUD

Installing the mongo Shell

Compass no cobre toda las opciones que da MongoDB

windows

osx/linux

conexión RS curso

mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/test?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl --username m001-student --password m001-mongodb-basics

Lab2

Connecting to your sandbox cluster from mongo shell

Loading data into your sandbox cluster

loadmoviedetailsdataset.zip

Connecting to your sandbox cluster from Compass

Creating Documents

insertOne()

desde Compass:

desde mongodb-shell:

insertMany()

db.moviesScratch.insertMany(
    [
        {
  	    "_id" : "tt0084726",
  	    "title" : "Star Trek II: The Wrath of Khan",
  	    "year" : 1982,
  	    "type" : "movie"
          },
          {
  	    "_id" : "tt0796366",
  	    "title" : "Star Trek",
  	    "year" : 2009,
  	    "type" : "movie"
          },
          {
  	    "_id" : "tt0084726",
  	    "title" : "Star Trek II: The Wrath of Khan",
  	    "year" : 1982,
  	    "type" : "movie"
          },
          {
  	    "_id" : "tt1408101",
  	    "title" : "Star Trek Into Darkness",
  	    "year" : 2013,
  	    "type" : "movie"
          },
          {
  	    "_id" : "tt0117731",
  	    "title" : "Star Trek: First Contact",
  	    "year" : 1996,
  	    "type" : "movie"
        }
    ]
);

insertmany.zip

Lab 2.2

Reading documents: scalar fields

ejemplo/quiz

buscar en movieDetails cuantas películas han ganado 2 galardones y han tenido 2 nominaciones:

2.7 Array Fields

Cursors

Projections

UpdateOne()

db.movieDetails.updateOne({
    title: "The Martian"
  }, {
    $set: {
      poster: "http://..."
    }
  })

la respuesta será:{«acknowledged»: true, «matchedCount»: 1, «modifiedCount»: 1 }, donde:

db.movieDetails.updateOne({
    title: "The Martian"
  }, {
    $set: {
      "awards": {
        "wins": 8,
        "nominations": 14
      }
    }
  })

Update Operators

For update operations, update operators specify how to modify specific fields in documents matching a filter. Fields may be added, deleted, or have their value changed in some way. Update operators define what modifications to make with respect to one or more fields in matching documents.

el documento ahora tendrá un campo llamado reviews del tipo array con un objeto que contendrá los campos indicados (rating,reviewer, text)

let review = [
  "aadasdsada",
  "dsdadasdsadasdsa",
  "asdasfeadfsdafasfdsfasdfsda"
].join()
db.movieDetails.updateOne({
  title: "the martian"
},{
  $push: {
    reviews:{  
      $each: [{
        rating: 4.5,
        reviewer: "Spencer H.",
        text: review
      },{
        rating: 4.5,
        reviewer: "Spencer H.",
        text: review
      },{
        rating: 4.5,
        reviewer: "Spencer H.",
        text: review
      }]
    }
  }
})

añade tantos objetos en el array como hay

UpdateMany()

db.movieDetails.updateMany({
  rated: null
},{
  $unset: {
    rated: ""
  }
})

elimina el campo rated = null de todos los documentos

Upserts

let detail={[...]}
 
db.movieDetails.updateOne({
  "imdb.id": detail.imdb.id
},{
  $set: detail
},{
  upsert: true
})

2.14 replaceOne()

db.movieDetails.replaceOne({"imdb.id": detailDoc.imdb.id}, detailDoc);

Delete

loadreviewsdataset.zip