M001: MongoDB Basics Ch 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

___________ updates the document matching the filter such that all key value pairs in the update document are reflected in the new version of the document it is updating.

$set

Suppose you wish to update the value of the plot field for one document in our movieDetails collection to correct a typo. What update operator and/or modifier would you need to use to do this?

$set is the only operator or modifier required for a simple update operation such as this

If the collection video.myMovies is currently empty, how many documents would be inserted by the following call to insertMany(). db.myMovies.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" } ], { ordered: false } );

4 Specifying Ordered False, even though there was a duplicate key error, Insert Many continued inserting documents.

What is the last part of this query db.movies.find({genre: "Action, Adventure"}, {title:1, "_id: 0})

A projection where the results from the query will return just the title and no _id

What is the last part of this query db.movies.find({genre: "Action, Adventure"}, {title:1})

A projection where the results from the query will return just the title and the _id

What is a upsert?

An operation in which update operators can actually create new documents.

Why does the Mongo Shell always connect to the primary cluster?

Because it's the primary to which most reads are typically directed and t which all writes for a MongoDB cluster must be directed.

C.R.U.D operations

Create, Read, Update, Delete

show collections

Lists all the collections in the database in use.

How would you query the collection movieDetails from the video database to find out how many movies (documents) list "Western" second among its genres. Do for MogoShell and Compass. (genres data type is array)

MongoShell use video db.movieDetails.find({"genres.1": "Western"}).count() Compass {genres.1: "Western"}

How would you query the collection movieDetails from the video database to find out how many movies (documents) in the movieDetails collection list "Family" among its genres. Do for MogoShell and Compass.

MongoShell use video db.movieDetails.find({genres: "Family"}).count() Compass {genres: "Family"}

How would you query the collection movieDetails from the video database to find out how many movies(documents) are rated PG and have exactly 10 award nominations (nested query). Do for MogoShell and Compass.

MongoShell use video db.movieDetails.find({rated: "PG", "awards.nominations": 10}).count() Compass {rated: "PG", awards.nominations: 10}

How would you query the collection movieDetails from the video database to find out how many movies (documents) list just two writers: "Ethan Coen" and "Joel Coen", in that order. Do for MogoShell and Compass.

MongoShell use video db.movieDetails.find({writers: ["Ethan Coen", "Joel Coen"]}).count() Compass {writers: ["Ethan Coen", "Joel Coen"]}

load("something.js")

MongoShell is a fully equipped java script interpreter. Can use to load a database or two onto the cluster.

How would you query the collection movieDetails from the video database to find out how may movies (documents) have exactly 2 award wins and 2 award nominations (nested query). Do for MogoShell and Compass.

Mongoshell use video db.movieDetails.find({"awards.wins": 2, "awards.nominations":2}).count() Compass {awards.wins: 2, awards.nominations: 2}

______________ reduce network overhead and processing requirements by limiting the fields that are returned in results documents

Projections

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

Removes the rated field for all that are set as null. nifty for cleaning up data sets.

Mongo Shell

Text based MongoDB client. Provides a number a administrative functions. As well as supporting the MongoDB Query language.

db.movieDetails,updateOne({ "imdb.id": detail.imdb.id }, { $set:detail }, { upsert: true });

The process that I used to collect the data that I wanted to put into the movie details collection was such that I couldn't guarantee only retrieving movies not already present in the movie details collection. So what I decided to do was to make use of the $set operator. The net effect of this was that, if the document already existed, I essentially replaced it with exactly the same detail data. If it didn't already exist, updateOne performed an upsert. Using updateOne with the upsert option meant that I didn't need to first query the collection to see whether the document already existed and then do a second query to insert. I let MongoDB do all the work for me. The trick here is that I'm using this third parameter to updateOne, setting upsert to true. This means that if this filter doesn't match any documents in my collection, this document-- which, for the Martian example, would be this one-- will be inserted in the collection. And that's the basic idea for upserts. Update documents matching the filter. If there are none, insert the update document as a new document in the collection.

Important aspects to consider about replaceOne:

The replacement document cannot contain update operators. replaceOne will apply changes to only one document, the first found in the server that matches the filter expression, using the $natural order of documents in the collection.

Describe the purpose of update operators?

To specify how one or more fields should be modified in matching documents. 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.

mongo -- nodb

Use to verify Mongo is installed currently and path is good. It starts the Mongo shell without attempting to connect to a database.

A _____________ us essentially a pointer to the current location in a result set.

cursor

the find method returns a ___________

cursor

db.collection.find()

displays all documents in the database

Only ________________ in a cluster can accept writes and for any cluster, there is one and only one primary.

primaries

The Mongo Shell will always connect to the ______________ for a cluster.

primary

show dbs

shows what databases are currently in the cluster.

use database

switch to the specific database

In the MongoDB query language, the method for updating a single document is called ______________.

updateOne

Why would you use replaceOne instead of updateOne?

updateOne is designed to update a specific set of values into a document replace one will completely replace the document

How would you do.this {"wind.type": "C"} (query a nested document) in the mongo shell (database is 100YWeather and collection is data)

use 100YWeather db.data.find({"wind.type": "C"}).pretty()

how would you do this {mpaaRating : "PG-13", year:2009} in the mongo shell (database is video and collection is movies)

use video db.movies.find({mpaaRating: "PG-13"}).pretty()

3 of the 4 crud operations are ______ opperations

write


Conjuntos de estudio relacionados

Pénzügyi és Biztosítási piac szereplői

View Set

CH 5 cultural awareness and health practices

View Set

Mastering Biology for test 2 + Clicker

View Set

CSCI361 Operating Systems Chapter 5

View Set

Business Statistics (True/False) II

View Set

biology test 3: the endocrine system

View Set