mongo Flashcards
<p>Get current db?</p>
<p>> db<br></br>
| test</p>
<p>Switch db?</p>
<p>> <strong>use</strong> video<br></br>
| switched to db video</p>
<p>Access collection from db?</p>
<p></p>
<p>You may access collections from the db variable.</p>
<p>> db.movies</p>
<p><strong>db </strong>always points to current database</p>
<p>How to show collections?</p>
<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>
<p>How to create db?</p>
<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>
<p>How to insert document?</p>
<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>
<p>How to find document?</p>
<p>>db.movies.<strong>find</strong>({})<br></br>
>db.movies.<strong>findOne</strong>({})</p>
<p>Select multiple documents and single document</p>
<p></p>
<p>How to update document?</p>
<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>
<p>How to delete documents?</p>
<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>
<p>What types does mongo supports?</p>
<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>
<p>What is the relation between ObjectId and _id?</p>
<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>
<p>What is the ObjectId?</p>
<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>
<p>How to remove all documents from the collection?</p>
<p>> db.movies.<strong>deleteMany</strong>({})<br></br>
> db.movies.<strong>drop</strong>()</p>
<p></p>
<p>How changes are made to documents in concurrent?</p>
<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>
<p>How to replace a document?</p>
<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>
<p>What are update operators?</p>
<p></p>
<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>
<p>What is$set update operator used for?</p>
<p></p>
<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>