MongoDB Query Language

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Querying with the ___ operator is useful when dealing with highly unstructured data where data types are not predictable

$type

What is contained in the second argument of the insertMany method()?

An optional document that contains the boolean ordered field

What is the second argument in the find() method?

An optional projection document that specifies the fields to return in the documents that match the query criteria.

$or, $and and $nor operators take a(n) ___ as a value.

Array

$type

Element operator that matches documents that have a particular value type for the specified field.

$min

Field update operator that only updates the field if the specified value is less than the existing field value

$unset

Field update operator that removes the specified field from a document

$rename

Field update operator that renames a field

$set

Field update operator that sets the value of a field in a document

What are the arguments used in the delete methods?

Filter and options EX: db.collection.deleteOne({filter},{options})

Where in the update operation can you find the upsert?

In the options document.

insertOne()

Inserts a single document into a collection

insertMany()

Inserts multiple documents into a collection

What happens when you set the upsert option to true with the replaceOne() method?

It would insert the replacement document as a new document if no match is found.

To limit the amount of data that MongoDB sends to applications, include a ___ to specify or restrict fields to return.

Projection document

Delete operations

Remove documents from a collection

replaceOne()

Replaces the first document in the collection that matches the filter using the replacement document

How can you update the first document in a collection without any selection criterion?

Specify an empty document {} as the filter

T or F: By default, the _id field is returned in matching documents when using the find() method.

TRUE

T or F: If you use the $inc operator on a field with a null value, it will generate an error.

TRUE

T or F: The filter document in an update operation uses the same syntax as the query document in the find( ) method.

TRUE

$mul

Field update operator that multiplies the value of the field by the specified amount

$max

Field update operator that only updates the field if the specified value is greater than the existing field value

What methods are used by MongoDB to insert documents into a collection?

db.collection.insertOne(); db.collection.insertMany();

What are the arguments used in the replaceOne( ) method?

Filter, replacement and options EX: db.collection.replaceOne( {filter}, {replacement}, {options})

What are the arguments used in the updateMany( ) method?

Filter, update and options EX: db.collection.updateMany( {filter}, {update}, {options})

What are the arguments used in the updateOne( ) method?

Filter, update and options EX: db.collection.updateOne( {filter}, {update}, {options})

If a new document is inserted using the upsert option, the new document fills in field values from ___ and the remaining fields from ___.

The filter criteria... the update operator

What is the syntax used to combine two filters in to one expression with the implicit AND operation?

db.collection.find({<field>: {$operator: <value>, $operator: <value>}});

What is the method used in MongoDB to perform queries in a collection?

db.collection.find({query},{project});

Update operations

Modify documents in a MongoDB collection

What is the result of the following query? db.movies.find({runtime: {$gt: 90}}, {_id: 0, title: 1, runtime: 1});

This finds movies whose runtime is greater than 90. For the matching movies, it will project the title and runtime fields and suppress the _id field.

The __ type returns documents with an number type including double, integer, or long.

"number"

What comparison operator is the equivalent of {<field>: <value>}?

$eg

What are the only ways to project specific elements of an array?

$elemMatch,$slice and $

What is the format used to write a query that returns only specified field(s) that are in an embedded document?

db.collection.find({searchKey: "desiredValue"}, {"embeddedDoc.field": 1});

The values of $in operator MUST be presented in __.

... an array

The $ne operator also returns documents that ___.

...do not have the named field.

What are the three parts used in a MongoDB query?

1. db; which refers to the currently connected database 2. a collection; which indicates the collection within the current database to run the query against 3. a method*; which specifies the action that the query will take EX: db.collection.method() * NOTE: each method has its own syntax given the arguments in parentheses

What are the methods used to update a collection?

1. updateOne( ) 2. updateMany( ) 3. replaceOne( )

IntelliShell

A built-in mongo shell in Studio3T to write and execute queries

What is returned when the find() method is executed?

A cursor

Cursor

A pointer to a current location in the result set

What does the editor section in IntelliShell do?

Allows you to write command line queries

What is contained within the first argument of the insertMany() method?

An array of documents to insert

$elemMatch

Array operator that selects documents if an element in the array field matches all the specified $elemMatch conditions.

$size

Array operator that selects documents if the array field is a specified size.

$all

Array operator used to match arrays that contain all elements specified in the query.

$[]

Array update operator that acts as a placeholder to update all elements in an array for the documents that match the query conditon

$

Array update operator that acts as a placeholder to update the first element that matches the query condition

$push

Array update operator that adds an item to an array

$addToSet

Array update operator that adds elements to an array only if they do not already exist

$pull

Array update operator that removes all array elements that match a specified query

$pop

Array update operator that removes the first or last item of an array

What conditions must a document meet to be a match for the $not logical operator?

The field does not exist OR the expression is not satisfied.

Why do we specify the collection name before we issue a method?

Because all CRUD operations in MongoDB target a single collection

How do we ensure a result set explicitly excludes fields when using the find() method?

By setting the fields to 0 in the projection document EX: db.movies.find({actors: "John Travolta"},{runtime:0, rated:0});

How do we ensure a result set explicitly includes fields when using the find() method?

By setting the fields to 1 in the projection document. EX: db.movies.find({genres: ["Action","Adventure"]},{title:1});

How to ensure you that you remove the correct document when using the delete operation?

By using the _id field when applying your filter to the collection via the first (filter) document of the delete operation.

T or F: You must always use quotation marks around the field name in the query argument of the find() method.

FALSE; you only use quotation marks around the field name in a query document when: 1. You are searching for a value in an embedded document 2. You are searching for a specific element of an array field

$ne

Comparison operator that matches all values that are not equal to a specified value

$in

Comparison operator that matches any of the values specified in an array

$nin

Comparison operator that matches none of the values in a specified array

$gt

Comparison operator that matches values that are greater than a specified value

$gte

Comparison operator that matches values that are greater than or equal to a specified value

$lt

Comparison operator that matches values that are less than a specified value

$lte

Comparison operator that matches values that are less than or equal to a specified value

$eg

Comparison operator used to match values that are equal to a specified value; equivalent to {<field>: <value>}

deleteMany()

Deletes all documents matching the filter db.collections.deleteMany({filter},{options})

deleteOne()

Deletes the first document matching the filter db.collections.deleteOne({filter}, {options})

What does the result section in IntelliShell do?

Displays results from your queries

$exists

Element operator that matches documents that have the specified field, including documents where the field value is null.

How do you avoid a duplicate key error when inserting a document that contains an _id field?

Ensure that the value for the _id field is unique within the collection

Unordered Insert

Executed by setting the value for ordered as FALSE in the insertMany() method; this orders MongoDB to keep inserting remaining documents after encountering an error.

Ordered Insert

Executed by setting the value for ordered as TRUE in the insertMany() method; this ensures that MongoDB will stop inserting documents as soon as it encounters an error. Note that this is the default behavior.

T or F: A query that uses the $or operator more than once can be constructed using the implicit AND operation.

FALSE; A query that uses the $or operator more than once cannot be constructed using an implicit AND operation.

T or F: When using the insertMany() method, you need must include the document that contains the ordered field set to TRUE to achieve an ordered insert.

FALSE; Because having an ordered insert is the default behavior in MongoDB, you do NOT need to specify this optional document for an ordered insert.

T or F: When used with the $not operator, comparison operators only match documents where the field exists or the expression is satisfied.

FALSE; Comparison operators only match documents where the field exists AND the expression is satisfied?

T or F: When using the $all operator, all elements included must appear in the order listed in the query to be shown in the result set.

FALSE; Elements included with the $all operator can be returned in any order as long as all elements are included in the array of the specified field.

T or F: If the field does not exist in matching documents, then $unset returns an error.

FALSE; If the field does not exist in matching documents, then $unset does nothing

T or F: It is easier to use the $or operator rather than the $in operator when performing an equality check on the same field.

FALSE; It is easier to use the $in operator when performing an equality check on the same field.

T or F: The $not operator must take an array as a value.

FALSE; The $not operator takes a document consisting of an expression as a value. {<field>: {$not: {<expression>}}}

T or F: The $set operator will return an error if the field specified in the update document of an update operation does not exist.

FALSE; it will create the new field with its corresponding value. Otherwise, the set operator will simply modify the value. (NOTE: this is also true of the $inc update operator)

T or F: All arguments in a MongoDB update operation are required.

FALSE; only the first and second arguments (filter and update) are required, while the third options argument is optional.

T or F: You cannot ever combine inclusion and exclusion statements in a projection document.

FALSE; you can combine inclusion and exclusion statements in a projection document IF and ONLY if you are excluding the _id field and then including other fields. EX: db.movies.find({actors: "John Travolta"},{title:1, _id:0});

T or F: To find a document in MongoDB that matches multiple conditions for different fields, you must use an array within the query argument of the find() method.

FALSE; you list each criterion as a field within the query argument.

$inc

Field update operator that increments the value of the field by the specified amount

What is the syntax used with comparison operators in MongoDB?

In a query document, we first name the field for comparison. Then, we specify the operator and the value of the field for comparison in an embedded document {<field>: {$operatorname: <value>} }

$not

Logical operator that inverts the effect of a query expression and returns documents that do not match the query expression.

$and

Logical operator that joins query clauses with a logical AND returns all documents that match the conditions of both clauses

$nor

Logical operator that joins query clauses with a logical NOR returns all documents that fail to match both clauses

$or

Logical operator that joins query clauses with a logical OR returns all documents that match the conditions of either clause

The expressions connected by a logical operator are ___ and each expression is ___.

The elements of an array ...a document itself

What is the easiest way to ensure that all fields in a document are included in the result set when using the find() method?

Omitting the second projection document

Read operations

Operations used in MongoDB to retrieve documents from collections.

T or F: The filter document in the delete operations uses the same syntax as the query document in the find() method.

TRUE

T or F: We can use upsert with the updateOne(), updateMany() and replaceOne() methods

TRUE

T or F: When using the $all operator, elements not listed in the query can still appear in the result set if they do not conflict with the query criteria.

TRUE

T or F: You cannot project a specific array element using an array index (i.e., {"actors.0":1})

TRUE

T or F: You should use the $elemMatch only when you have multiple query conditions in the $elemMatch expression.

TRUE; If you have only a single query condition in the $elemMatch expression, you do not need to use $elemMatch.

To specify a field in an embedded document or array, use ___.

The dot notation

What are the required arguments of the insertMany() method? What (if any) are optional?

The first argument containing the array which lists the documents to be added to the collection is mandatory. The second argument, which specifies the value for the ordered field, is not required.

Filter document

The first argument in an update operation that is used to locate the documents to update

What determines which documents are returned when using the find() method?

The first argument, which is a document specifying the query criteria.

What are the required arguments of the find() method? What (if any) are optional?

The first query method is not required. It specifies the search conditions is required. The second projection document, which specifies which fields to include in the result set, is not required.

What happens if you try to insert a document into a collection that doesn't exist?

The insert operations create the selection

What is the result of the following query? db.movies.find({ metacritic: {$not: {$gt: 88}} }, {title: 1, metacritic: 1});

The query will select all documents in the movies collection where the metacritic field value is less than or equal to 88, or metacritic field does not exist.

Update document

The second argument in an update operation that is used to describe the changes being made to the documents that are found

What is the result of the following query? db.movies.find({"tomato.meter": {$type: "double"}});

This checks whether the meter field in the tomato embedded document is a double.

What is the result of the following query? db.movies.find({"tomato.meter": {$type: "int"}});

This checks whether the meter field in the tomato embedded document is an integer.

What is the result of the following query? db.movies.find({rated: {$exists: true}});

This finds movies which contain the rated field, including values where the rated field value is null.

What is the result of the following query? db.movies.find({rated: {$exists: false}});

This finds movies which do NOT contain the rating field.

What is the result of the following query? db.movies.find({rated: null}, {_id: 0, title: 1, rated: 1});

This query allows you to check which movies do not have a rated field.

What is the result of the following query? db.movies.find({rated: {$in: ["G", "PG"]}}, {_id: 0, title: 1, rated: 1});

This query finds all documents in the movies collection where the rated field is either G or PG.

What is the result of the following query? db.movies.find({genres: {$all: ["Comedy", "Drama" , "Crime"]}}, {_id: 0, title: 1, genres: 1});

This query finds all movies with comedy, drama, and crime as part of their genres. Note that the array must contain all three elements and these elements can be in any order. In addition, there can be other elements apart from these three elements.

What is the result of the following query? db.movies.find({countries: {$size: 3}});

This query finds movies in which countries array has three elements, implying that the movie was shot in three different countries.

What is the result of the following query? db.movies.find({rated: {$ne: "UNRATED"}}, {_id: 0, title: 1, rated: 1});

This query finds movies where the rated field contains a value that is different from UNRATED (R, PG-13, etc.). It will also return values that do not contain the rated field.

What is the result of the following query? db.movies.find({metacritic: {$gt: 88}, metacritic: {$ne: 100}});

This query results in finding documents that match the last expression. That is, only the last expression will be considered.

What is the result of the following query? db.movies.find({$or: [{"tomato.meter": {$gt: 95}}, {metacritic: {$gt: 88}}]}, {title: 1, "tomato.meter": 1, metacritic: 1});

This query return movies that either have a score greater than 95 in the meter field that is embedded within the tomato document or have a value greater than 88 in the metacritic field. It will display the title, tomato.meter and metacritic fields.

What is the result of the following query? db.movies.find({"tomato.meter": {$gt: 95}, metacritic: {$gt: 88}}, {title: 1, "tomato.meter": 1, metacritic: 1});

This query return movies that have a score greater than 95 in the meter field that is embedded within the tomato document AND have a value greater than 88 in the metacritic field. It will display the title, tomato.meter and metacritic fields.

What is the result of the following query? db.movies.find({$and: [{"tomato.meter": {$gt: 95}}, {metacritic: {$gt: 88}}]}, {title: 1, "tomato.meter": 1, metacritic: 1});

This query return movies that have a score greater than 95 in the meter field that is embedded within the tomato document AND have a value greater than 88 in the metacritic field. It will display the title, tomato.meter and metacritic fields.

What is the result of the following query? db.movies.find({ metacritic: {$lte: 88} }, {title: 1, metacritic: 1});

This query returns only those documents where the field exists and it is less than or equal to 88.

What is the result of the following query? martian.boxOffice = [ {country: "USA", revenue: 228.4}, {country: "Australia", revenue: 19.6}, {country: "UK", revenue: 33.9}, {country: "Germany", revenue: 16.2}, {country: "France", revenue: 19.8}]; db.movies.find({"boxOffice.country": "Germany", "boxOffice.revenue": {$gt: 17}});

This query searches for movies and returns documents that have a value of Germany in the boxOffice.country field and a value of boxOffice.revenue greater than 17 in ANY of the array elements.

What is the result of the following query? martian.boxOffice = [ {country: "USA", revenue: 228.4}, {country: "Australia", revenue: 19.6}, {country: "UK", revenue: 33.9}, {country: "Germany", revenue: 16.2}, {country: "France", revenue: 19.8}]; db.movies.find({boxOffice: {$elemMatch: {country: "Germany", revenue: {$gt: 17}}}});

This query searches for movies where the boxOffice array has an element in which the county field value is Germany and the revenue field value is greater than 17.

What is the result of the following query? db.movies.find({$and: [ {$or: [{"tomato.meter": {$gt: 95}}, {metacritic: {$gt: 90}}]}, {$or: [{"awards.wins": {$gt: 30}}, {"awards.nominations": {$gt: 40}}]}]});

This query will select all documents where the tomato.meter field value is greater than 95 or the metacritic field value is greater than 90, AND the awards,wins field is greater than 30 or the awards.nominations field value is greater than 40.

What is the result of the following query? db.movies.find({runtime: {$gt: 90, $lt: 120}}, {_id: 0, title: 1, runtime: 1});

This searches for movies whose runtime is greater than 90 and less than 120. It suppresses the _id field from the result set and shows title and runtime.

What will the following query return in its result set? db.movies.find();

This will return an empty document.

What will the following query return in its result set? db.movies.find({});

This will return everything in the movies collection.

What is the result of the following query? db.movies.find({runtime: {$gte: 180}, "tomato.meter": {$lte: 100}}, {_id: 0, title: 1, runtime: 1});

This will search for movies that have a run time greater than or equal to 180 AND a meter value in the tomato document that is less than 100. It will suppress the _id field and show title and runtime of the result set.

What values can the $exist operator take?

True or false

updateMany()

Updates all documents in the collection that match the filter

updateOne()

Updates the first document in the collection that matches the filter

You want to insert a new movie to the movies collection, but you don't know if the movie already exists in the collection. What do you do?

Use the updateOne() method with the upsert option

find()

Use to perform queries within a collection, similar to the SELECT statement in SQL

Array operators

Used in conjuction with array fields in queries.

Element operators

Used to ask meta questions about documents

Logical operators

Used to connect multiple conditions or expressions.

How do you filter for any SINGLE element of an array field?

We specify the criterion as if we are making a search on a top-level field. This will match all documents that have a matching value within the specified field array. EX: db.movies.find({actors: "John Travolta"});

How do you find a document that matches a filter on a top-level field in MongoDB?

We specify the criterion on the query document using a field-value pair EX: db.movies.find({rated: "PG-13"});

How do you find a document that matches a filter on multiple top-level fields in MongoDB?

We specify the criterion using a field-value pair in the query document separated by a comma EX: db.movies.find({rated: "PG-13", year: 2009});

How do you look for a match in a specific element of an array field?

We specify the field with an array index (starting with zero) in the dot notation and in quotation marks. EX: db.movies.find({"actors.0": "John Travolta"});

How do you find a document that matches a filter on an embedded document?

We specify the nested field in the dot notation and in quotation marks EX: db.moves.find({"imbd.rating":8.6});

How do you filter for ALL elements of an array field, including the order of the elements?

We specify the values in an array; the result will match all documents in which the fields has the exact same number of and values for the elements listed in the array. EX: db.movies.find({writers: ["Charles McKeown", "Terry Gilliam"]});

When is it necessary to use an explicit AND with the $and operator?

When either: 1. The same field has to be specified in multiple expressions (first image) 2. The same operator has to specified in multiple expressions

What happens when you include upsert: true in your update operation?

When no documents match the specified filer, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies the matching documents.

When does MongoDB provide an implicit AND operation?

When specifying a comma-separated list of expressions. db.collection.find({<field>: {$operator: <value>}, <field>: {$operator: <value>}}, {<field>: 1, <field>: 1});

How do you use multiple comparison operators on multiple fields?

You list the value and comparison operator for as its own document after the associated field. db.collection.find({<field>: {$operatorname: <value>}, <field>: {$operatorname : value}}, {_id: 0});

How do you query a field using multiple comparison operators?

You put a comma between the comparison operators and their respective values in the query document. db.collection.find({<field>: {$operatorname: <value>, $operatorname : value}}, {_id: 0});

If a document does not specify a(n) ___ field, MongoDB adds it with an ObjectId value.

_id

What methods does MongoDB provide to delete documents of a collection?

db.collection.deleteOne({filter}, {options}) db.collection.deleteMany({filter}, {options})

What syntax do you use when you want to use the $elemMatch operator to find documents where the same array element satisfies all conditions?

db.collection.find({<field>: {$elemMatch: {<field>: <value>, <field>: {$compoperator: <value>}});

MongoDB provides the ___ method to perform queries in a collection.

find()

What is the syntax used with $or, $and and $nor logical operators?

{$or: [{<expression>}, {<expression>}, ...,{<expression>}]}


Ensembles d'études connexes

Chapter 16 Real Estate Appraisal

View Set

2020 Computer Architecture Final

View Set

ECON: Oligopoly/Game theory Terms

View Set

Legal Concepts of the Insurance Contract

View Set

Algebra 2 Honors Semester 1 Review

View Set

Week 1: Nature of Anthropology, Sociology, and Political Science

View Set

A separate peace chapter 5-10 discussion quiz

View Set