mongo Flashcards

1
Q

<p>Get current db?</p>

A

<p>> db<br></br>

| test</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

<p>Switch db?</p>

A

<p>> <strong>use</strong> video<br></br>

| switched to db video</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

<p>Access collection from db?</p>

<p></p>

A

<p>You may access collections from the db variable.</p>

<p>> db.movies</p>

<p><strong>db </strong>always points to current database</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

<p>How to show collections?</p>

A

<p>show collections command</p>

<p>> use local</p>

<p>switched to db local</p>

<p>>show collections</p>

<p>startup_log</p>

<p>> db.startup_log.find({})</p>

<p></p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

<p>How to create db?</p>

A

<p><strong>use</strong> command with new db name</p>

<p>>use testdb</p>

<p>switched to db testdb</p>

<p>will create db</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

<p>How to insert document?</p>

A

<p><strong>insertOne()</strong></p>

<p><strong>insertMany()</strong></p>

<p>>db.movies.insertOne({ "title" : "Star Wars: Episode IV - A New Hope", "director" : "George Lucas", "year" : 1977 })</p>

<p></p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

<p>How to find document?</p>

A

<p>>db.movies.<strong>find</strong>({})<br></br>
>db.movies.<strong>findOne</strong>({})</p>

<p>Select multiple documents and single document</p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

<p>How to update document?</p>

A

<p><strong>updateOne</strong>() - Updates one single document<br></br>
<strong>updateMany</strong>() - Updates all documents matching criteria</p>

<p></p>

<p>Requires at least two parameters</p>

<p>First - search criteria<br></br>
Second - document with operators describing how to update document</p>

<p></p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

<p>How to delete documents?</p>

A

<p>deleteOne() - Delete single document<br></br>
deleteMany() - Delete multiple documents</p>

<p>Both methods take a filter document specifying criteria for the removal.</p>

<p></p>

<p></p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

<p>What types does mongo supports?</p>

A
<ol>
	<li>Null
	<ol>
		<li>{"x" : null}</li>
	</ol>
	</li>
	<li>Boolean
	<ol>
		<li>{"x" : true}</li>
	</ol>
	</li>
	<li>Number
	<ol>
		<li>{"x" : 3.14}</li>
	</ol>
	</li>
	<li>String
	<ol>
		<li>{"x" : "foobar"}</li>
	</ol>
	</li>
	<li>Date
	<ol>
		<li>{"x" : new Date()}</li>
	</ol>
	</li>
	<li>Regular expression
	<ol>
		<li>{"x" : /foobar/i}</li>
	</ol>
	</li>
	<li>Array
	<ol>
		<li>{"x" : ["a", "b", "c"]}</li>
	</ol>
	</li>
	<li>Embedded document
	<ol>
		<li>{"x" : {"foo" : "bar"}}</li>
	</ol>
	</li>
	<li>Object ID
	<ol>
		<li>{"x" : ObjectId()}</li>
	</ol>
	</li>
	<li>Binary data
	<ol>
		<li>Binary data is a string of arbitrary bytes. It cannot be manipulated from the shell. Binary data is the only way to save non-UTF-8 strings to the database</li>
	</ol>
	</li>
	<li>Code
	<ol>
		<li>MongoDB also makes it possible to store arbitrary JavaScript in queries and documents:<br>
		{"x" : function() { /* ... */ }}</li>
	</ol>
	</li>
</ol>

<p>Json doesn't support data types:</p>

<p>a function<br></br>
a date<br></br>
undefined</p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

<p>What is the relation between ObjectId and _id?</p>

A

<p>Every document stored in MongoDB must have an "_id" key.<br></br>
The "_id" key’s value can be any type, but it defaults to an ObjectId.</p>

<p>In a single collection, every document must have a unique value for "_id", which ensures that every document in a collection can be uniquely identified. That is, if you had two collections, each one could have a document where the value for "_id" was 123.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

<p>What is the ObjectId?</p>

A

<p>ObjectId is the default type for "_id".</p>

<p>The ObjectId class is designed to be lightweight, while still being easy to generate in a globally unique way across different machines.</p>

<p>MongoDB’s distributed nature is the main reason why it uses ObjectIds as opposed to something more traditional, like an autoincrementing primary key: it is difficult and time-consuming to synchronize autoincrementing primary keys across multiple servers. Because MongoDB was designed to be a distributed database, it was important to be able to generate unique identifiers in a sharded environment.</p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

<p>How to remove all documents from the collection?</p>

A

<p>> db.movies.<strong>deleteMany</strong>({})<br></br>
> db.movies.<strong>drop</strong>()</p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

<p>How changes are made to documents in concurrent?</p>

A

<p>Updating a document is atomic: if two updates happen at the same time, whichever one reaches the server first will be applied, and then the next one will be applied. Thus, conflicting updates can safely be sent in rapid-fire succession without any documents being corrupted: the last update will “win.”</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

<p>How to replace a document?</p>

A

<p>replaceOne()</p>

<p>replaceMany() doesn't exist.</p>

<p>> db.people.replaceOne({"name" : "joe"}, {"name": "vasya"});</p>

<p>Accepts two parameters:</p>

<ol>
<li>Search criteria</li>
<li>Document for a replacement</li>
</ol>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

<p>What are update operators?</p>

<p></p>

A

<p>Usually only certain portions of a document need to be updated. You can update specific fields in a document using atomic<strong> update operators.</strong> <strong>Update operators </strong>are special keys that can be used to specify complex update operations, such as altering, adding, or removing keys, and even manipulating arrays and embedded documents.</p>

<p></p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

<p>What is$set update operator used for?</p>

<p></p>

A

<p>"$set" sets the value of a field. If the field does not yet exist, it will be created. This can be handy for updating schemas or adding user-defined keys.</p>

<p>1</p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

<p>What is$unset update operator used for?</p>

A

<p>The unset operator is used to remove a key.</p>

<p></p>

<p></p>

19
Q

<p>$inc update operator</p>

A

<p>The"$inc"operatorcan be used to <strong>change the value for an existing key</strong> or to <strong>create a new key</strong> if it does not already exist.</p>

<p>Increments or decrements value or sets new value</p>

<p>"$inc"can be used only on values of type number or will give an error.</p>

<p></p>

<p></p>

<p></p>

20
Q

<p>$push update operator?</p>

A

<p>"$push"<strong>adds elementsto the end </strong>of an array if the array exists and <strong>creates a new array</strong> if it does not.</p>

<p></p>

<p></p>

21
Q

<p>which modifiers $push operator uses?</p>

A

<ol>
<li>
<p>$each</p>

~~~
<ol>
<li>
<p>Appends multiple values to the array field.</p>
</li>
</ol>
</li>
<li>
<p>$slice</p>
~~~

~~~
<ol>
<li>
<p>Limits the number of array elements. Requires the use of the$eachmodifier.</p>
</li>
</ol>
</li>
<li>
<p>$sort</p>
~~~

~~~
<ol>
<li>
<p>Orders elements of the array. Requires the use of the$eachmodifier.</p>
</li>
</ol>
</li>
<li>
<p>$position</p>
~~~

~~~
<ol>
<li>
<p>Specifies the location in the array at which to insert the new elements. Requires the use of the$eachmodifier. Without the$positionmodifier, the$pushappends the elements to the end of the array.</p>
</li>
</ol>
</li>
</ol>

~~~

<p></p>

22
Q

<p>$addToSet update operator?</p>

<p></p>

A

<p>Arrayscan be treated as a set</p>

<p>The$addToSetoperator adds a value to an array unless the value is already present, in which case$addToSetdoes nothing to that array.</p>

<p>{ $addToSet: { : , ... } }</p>

<p>If you want to add multiple values you can use $each modifier.<br></br>
Otherwise, an array will be added as is resulting in nested array</p>

<p></p>

<p></p>

23
Q

<p>$pop update operator?</p>

A

<p>The<strong>$pop</strong>operator removes the first or last element of an array. Pass$popa value of-1to remove the first element of an array and1to remove the last element in an array.</p>

<p></p>

<p></p>

24
Q

<p>$pull operator?</p>

A

<p>The$pulloperator removes from an existing array all instances of a value or values that match a specified condition.</p>

<p>{ $pull: { : , : , ... } }</p>

<p></p>

<p></p>

25
Q

<p>How we can modify an element by index without knowing the actual index?</p>

<p></p>

A

<p>In many cases, though, we don’t know what index of the array to modify without querying for the document first and examining it. To get around this, MongoDB has a positional operator,<strong>$</strong>, that figures out which element of the array the query document matched and updates that element.</p>

<p></p>

<p></p>

26
Q

<p>How we can make upsert?</p>

A

<p>Anupsertis a special type of update. If no document is found that matches the filter, a new document will be created by combining the criteria and updated documents.</p>

<p>It allows atomicity and speed because of less amount of requests responses.</p>

<p>In this case if there is no document it will be created. with the base of find operator and then $inc will be applied</p>

27
Q

<p>$setOnInsert operator?</p>

A

<p>If an update operation withupsert: trueresults in an insert of a document, then$setOnInsertassigns the specified values to the fields in the document. If the update operation does not result in an insert,$setOnInsertdoes nothing.</p>

<p></p>

<p>Only for upsert operations.</p>

<p></p>

28
Q

<p>What is save()?</p>

A

<p>save() is a shell function that lets you insert a document if it doesn’t exist and update it if it does. It takes one argument: a document. If the document contains an"_id"key,savewill do an upsert.</p>

<p></p>

29
Q

<p>whataremethods</p>

<p>findOneAndUpdate()<br></br>
findOneAndReplace()<br></br>
findOneAndDelete()</p>

A

<p>Those are methods that can find the document, perform update/replace or deletion returning the document before or after modification.</p>

<p></p>

30
Q

<p>How to add conditions to search criteria?</p>

A

<p>Multiple conditionscan be strung together by adding more key/value pairs to the query document, which gets interpreted as condition1ANDcondition2AND … ANDconditionN.” For instance, to get all users who are 27-year-olds with the username “joe,” we can query for the following:</p>

<p>> db.users.find({"username" : "joe", "age" : 27})</p>

<p></p>

31
Q

<p>How we can add projection to find?</p>

A

<p>As a second parameter to find()</p>

<p>> db.users.find({}, {"username" : 1, "email" : 1})</p>

<p>to remove a field from the result the following option</p>

<p>> db.users.find({}, {"fatal_weakness" : 0})</p>

<p></p>

32
Q

<p>How to express query conditionals <= , >=</p>

A
<p>> db.users.find({"age" : {"$gte" : 18, "$lte" : 30}})<br>
<br>
> start = new Date("01/01/2007")<br>
> db.users.find({"registered" : {"$lt" : start}})</p>

<p></p>

<p>> db.users.find({"username" : {"$ne" : "joe"}})</p>

<p></p>

33
Q

<p>How we can implement OR queries?</p>

<p></p>

A

<p>1) using <strong>$in</strong></p>

<p>> db.raffle.find({"ticket_no" : {"$in" : [725, 542, 390]}})</p>

<p>2) using <strong>$nin</strong></p>

<p>> db.raffle.find({"ticket_no" : {"$nin" : [725, 542, 390]}})</p>

<p>3) using <strong>$or</strong></p>

<p>> db.raffle.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]})</p>

34
Q

<p>$not criteria condition</p>

A

<p>"$not"isa meta-conditional: it can be applied on top of any other criteria. As an example, let’s consider themodulus operator,"$mod"."$mod"queries for keys whose values, when divided by the first value given, have a remainder of the second value:</p>

35
Q

<p>null in find criteria</p>

A

<p>It does match itself</p>

<p>> db.c.find({"y" : null})<br></br>
{ "_id" : ObjectId("4ba0f0dfd22aa494fd523621"), "y" : null }</p>

<p><strong>but</strong></p>

<p>nullalso matches “does not exist.”<br></br>
Thus, querying for a key with the valuenullwill return all documents lacking that key</p>

<p>> db.c.find({"z" : null})</p>

<p>{ "_id" : ObjectId("4ba0f0dfd22aa494fd523621"), "y" : null } { "_id" : ObjectId("4ba0f0dfd22aa494fd523622"), "y" : 1 }</p>

<p>If we only want to find keys whose value isnull, we can check that the key isnulland exists using the"$exists"conditional:</p>

<p>> db.c.find({"z" : {"$eq" : null, "$exists" : true}})</p>

<p></p>

36
Q

<p>$regex in find criteria</p>

<p></p>

A

<p>"$regex"providesregular expression capabilities for pattern matching strings in queries.</p>

<p>> db.users.find({"name":{"$regex" : /joe/i }})</p>

<p>MongoDBcan leverage an index for queries on prefix regular expressions (e.g.,<strong>/^joey/</strong>). Indexes<strong>cannotbe used</strong> for case-insensitive searches (/^joey/i).</p>

<p></p>

37
Q

<p>How to query a single element in an array?</p>

A

<p>> db.food.find({"fruit" : "banana"})</p>

<p>will match</p>

<p>> db.food.insertOne({"fruit" : ["apple", "banana", "peach"]})</p>

<p>array with any element in it.</p>

<p></p>

38
Q

<p>“$ALL” in find criteria</p>

<p></p>

A

<p>> db.food.find({fruit : {$all : ["apple", "banana"]}})</p>

<p>{"_id" : 1, "fruit" : ["apple", "banana", "peach"]}<br></br>
{"_id" : 3, "fruit" : ["cherry", "banana", "apple"]}</p>

<p>match document with array containing all values in all</p>

39
Q

<p>How to match the entire array in find?</p>

A

<p>> db.food.find({"fruit" : ["apple", "banana", "peach"]})</p>

<p>will match document with array elements exactly the same (order matters)</p>

<p></p>

40
Q

<p>How to match an array element in the document?</p>

A

<p>> db.food.find({"fruit.2" : "peach"})</p>

<p>Arrays are always 0-indexed, so this would match the third array element against the string"peach".</p>

<p></p>

41
Q

<p>How to match by array size?</p>

<p>“$SIZE”</p>

A

<p>Auseful conditional for querying arrays is"$size", which allows you to query for arrays of a given size.</p>

<p>> db.food.find({"fruit" : {"$size" : 3}})</p>

<p>Works only for the exact match if you want to lt or gt need to store size in the field.</p>

42
Q

<p>What is $slice (projection)?</p>

A

<p>The$sliceprojection operator specifies the number of elements in an array to return in the query result.</p>

<p></p>

<p></p>

43
Q
A