Compass no cobre toda las opciones que da MongoDB
c:\Program Files\MongoDB\Server\3.4
mongo –nodb
→ quit()
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
use video
show collections
db.movies.find().pretty()
mongo "mongodb://sandbox-shard-00-00-cab0l.mongodb.net:27017,sandbox-shard-00-01-cab0l.mongodb.net:27017,sandbox-shard-00-02-cab0l.mongodb.net:27017/test?replicaSet=Sandbox-shard-0" --ssl --authenticationDatabase admin --username m001-student --password m001-mongodb-basics
show dbs
load(«loadMovieDetailsDataset.js»)
desde Compass:
desde mongodb-shell:
db
→ database en usodb.moviesScratch.insertOne({title:"Star Trek II: Thw Wrath of man", year: 1982, imdb: "TT0084726"})
db.moviesScratch.insertOne({_id: "tt0084726", title:"Star Trek II: Thw Wrath of man", year: 1982, imdb: "TT0084726"})
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" } ] );
{«campo1»: restricción1, «campo2»: restricción2}
{«objeto.campo»: valor1, «objeto.subobjeto.campo»: valor2}
buscar en movieDetails cuantas películas han ganado 2 galardones y han tenido 2 nominaciones:
db.movieDetails.find({«awards.wins»: 2, «awards.nominations»: 2}).count()
{awards.wins: 2, awards.nominations: 2}
db.movies.find({cast: [«Jeff Bridges», «Tim Robbins»]}).pretty()
: muestra solo los documentos que cumplen exactamente el critero de tener 2 elementos en el array *cast* con esos actoresdb.movies.find({cast: «Jeff Bridges»}).pretty()
: muestra los documentos donde aparece ese actor, aunque no exclusimante y sin tener importancia el ordendb.movies.find({«cast.0»: «Jeff Bridges»}).pretty()
: busca el actor en la primera posición del array *cast*it
find()
db.movies.find({genre: «Action,Adventure»},{title: 1})
: muestra solo el títulodb.movies.find({genre: «Action,Adventure»},{title: 1, _id:0})
: idem anterior, pero sin el campo _iddb.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 } } })
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.
let review = [ "aadasdsada", "dsdadasdsadasdsa", "asdasfeadfsdafasfdsfasdfsda" ].join() db.movieDetails.updateOne({ title: "the martian" },{ $push: { reviews: { rating: 4.5, reviewer: "Spencer H.", text: review } } })
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
db.movieDetails.updateMany({ rated: null },{ $unset: { rated: "" } })
elimina el campo rated = null de todos los documentos
let detail={[...]} db.movieDetails.updateOne({ "imdb.id": detail.imdb.id },{ $set: detail },{ upsert: true })
detailDoc = db.movieDetails.findOne({«imdb.id»: «tt….»});
detailDoc.poster;
: printea null, no está definidodetailDoc.poster = «http://.…»;
; añade el campodetailDoc.genres.push(«Documentary»);
este push es un método javascript, no el operador visto anteriormente, y añade el valor al array de genresdb.movieDetails.replaceOne({"imdb.id": detailDoc.imdb.id}, detailDoc);