MongoDB

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

Which are the most important features of MongoDB?

- Flexible data model in form of documents (schema-less) - Agile and highly scalable database (Sharding) - Faster than traditional databases - Expressive query language (dynamic, querying nested documents) - Avoids the "impedance mismatch" between object and relational models when a relational database is used with an object-oriented application.

What is a Namespace in MongoDB?

A Namespace is the concatenation of the database name and collection name. For e.g. school.students with school as the database and students as the collection

What is a covered query in MongoDB?

A covered query is the one in which: - fields used in the query are part of an index used in the query, and the fields returned in the results are in the same index

What's a Secondary or Slave?

A secondary is a node/member which applies operations from the current primary. This is done by tailing the replication oplog (local.oplog.rs). Replication from primary to secondary is asynchronous, however, the secondary will try to stay as close to current as possible (often this is just a few milliseconds on a LAN).

What is Aggregation in MongoDB?

Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. MongoDB provides three ways to perform aggregation: the aggregation pipeline, the map-reduce function, and single purpose aggregation methods and commands.

How many indexes does MongoDB create by default for a new collection?

By default, MongoDB creates the _id collection for every collection.

By default, MongoDB writes and reads data from both primary and secondary replica sets. True or False.

False. MongoDB writes data only to the primary replica set.

How do I see the status of locks on my mongod instances?

For reporting on lock utilization information on locks, use any of the following methods: db.serverStatus(), db.currentOp(), mongotop, mongostat, and/or the MongoDB Management Service (MMS) Specifically, the locks document in the output of serverStatus, or the locks field in the current operation reporting provides insight into the type of locks and amount of lock contention in your mongod instance. To terminate an operation, use db.killOp().

Explain what is GridFS in MongoDB?

For storing and retrieving large files such as images, video files and audio files GridFS is used. By default, it uses two files fs.files and fs.chunks to store the file's metadata and the chunks.

How does MongoDB sort queries in sharded environments?

If you call the cursor.sort() method on a query in a sharded environment, the mongod for each shard will sort its results, and the mongos merges each shard's results before returning them to the client.

How do you copy all objects from one collection to another?

In the mongo shell, you can use the following operation to duplicate the entire collection: db.source.copyTo(newCollection) Warning: When using db.collection.copyTo() check field types to ensure that the operation does not remove type information from documents during the translation from BSON to JSON. Consider using cloneCollection() to maintain type fidelity. The db.collection.copyTo() method uses the eval command internally. As a result, the db.collection.copyTo() operation takes a global lock that blocks all other read and write operations until the db.collection.copyTo() completes. Also consider the cloneCollection command that may provide some of this functionality.

What are Indexes in MongoDB?

Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.

What is the role of profiler in MongoDB?

MongoDB includes a database profiler which shows performance characteristics of each operation against the database. With this profiler you can find queries (and write operations) which are slower than they should be and use this information for determining when an index is needed.

What kind of NoSQL database is MongoDB?

MongoDB is a document oriented database. It stores data in the form of BSON structure based documents. These documents are stored in collections.

What is the method to configure the cache size in MongoDB?

MongoDB's cache is not configurable. Actually MongoDb uses all the free spaces on the system automatically by way of memory mapped files.

What is Mongoose?

Mongoose is a MongoDB object modeling tool, or ODM (Object Document Mapper), written in JavaScript and designed to work in an asynchronous environment

Does MongoDB database use tables for storing records?

No, Instead of tables, MongoDB uses "Collection" to store data.

Explain the structure of ObjectID in MongoDB.

ObjectID is a 12-byte BSON type with: - 4 bytes value representing seconds - 3 byte machine identifier - 2 byte process id - 3 byte counter

When creating a data model in MongoDB what are the points need to be taken in consideration?

Points need to be taken in consideration are - Design your schema according to user requirements - Combine objects into one document if you use them together. Otherwise, separate them - Do joins while write, and not when it is on read - Optimize your data model for the most important use cases - Do complex aggregation in the schema

What is Replication in MongoDB?

Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability. With multiple copies of data on different database servers, replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions.

Compare SQL databases and MongoDB at a high level.

SQL (relational) databases store data in form of tables, rows and columns. This data is stored in a pre-defined schema which is not very much flexible for today's real-world highly growing applications. MongoDB in contrast uses a flexible document structure which places the responsibility for data modeling mostly on the other parts of the application. A given collection may hold documents with different structure as long as the application is programmed to expect such variation.

What is Sharding in MongoDB? Explain.

Sharding is a method for storing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations.

What are different type of data types in mongodb?

String Number Date Buffer Boolean ObjectId Array

Which command can be used to provide various information on the query plans used by a MongoDB query?

The explain() command can be used for this information. The possible modes are: 'queryPlanner', 'executionStats', and 'allPlansExecution'.

To do Safe backups what features are used in MongoDB?

The journaling is the feature in MongoDB that you can use to do safe backups.

What happens when a document is updated on a chunk that is being migrated?

The update will go through immediately on the old Shard and then the change will be replicated to the new Shard before ownership transfers.

What are some different types of NoSQL databases?

There are four types of NoSQL database: Document store NoSQL database Graph base NoSQL database Key value store NoSQL database A column store NoSQL database

What's a Master or Primary?

This is a node/member which is currently the primary and processes all writes for the replica set. During a failover event in a replica set, a different member can become primary.

How do you determine the size of an index?

To check the sizes of the indexes on a collection, use db.collection.stats().

MongoDB uses BSON to represent document structures. True or False?

True

How does Journaling work in MongoDB?

When running with journaling, MongoDB stores and applies write operations in memory and in the on-disk journal before the changes are present in the data files on disk. Writes to the journal are atomic, ensuring the consistency of the on-disk journal files. With journaling enabled, MongoDB creates a journal subdirectory within the directory defined by dbPath, which is /data/db by default.

Are null values allowed?

Yes, but only for the members of an object. A null cannot be added to the database collection as it isn't an object. But {}can be added.

Is there a way to access the ObjectId constructor from Mongoose?

Yes, you can find the ObjectId constructor on require('mongoose').Types. Here is an example: var mongoose = require('mongoose'); var id = mongoose.Types.ObjectId(); id is a newly generated ObjectId.

If you remove an object attribute is it deleted from the store?

Yes, you remove the attribute and then re-save() the object if you use the replace, replaceOne, or replaceMany.

Can you create an index on an array field in MongoDB? If yes, what happens in this case?

Yes. An array field can be indexed in MongoDB. In this case, MongoDB would index each value of the array.

If you remove a document from database in MongoDB, does MongoDB remove it from disk?

Yes. If you remove a document from database, MongoDB will remove it from disk too.

How can I access different databases temporarily?

You can use db.getSiblingDB() method to access another database without switching databases, as in the following example which first switches to the test database and then accesses the sampleDB database from the test database: use test db.getSiblingDB('sampleDB').getCollectionNames();

When should we embed one document within another in MongoDB?

You should consider embedding documents for: - 'contains' relationships between entities - One-to-many relationships (if the many is not very large) - Performance reasons

(duplicate) What is the command syntax for inserting a document in MongoDB?

database.collection.insert(document)

(duplicate question - answer doesn't match question) What is the command syntax for inserting a document in MongoDB?

db.COLLECTION_NAME.ensureindex ( {KEY:1} ) In this key is the name of the on which you want to make an index, where 1 is for accessing sequence otherwise you use -1 for descending order.

How can I see the connections used by mongos?

db._adminCommand("connPoolStats");

What is the syntax to drop a collection in MongoDB?

db.collection.drop()

What is the command to list all the indexes on a particular collection.

db.collection.getIndexes()

What is the syntax to create a collection in MongoDB?

db.createCollection(name,options)

How would you find documents in the inventory collection that have a status of "A" and a qty < 30 or a name starting with the letter "p"?

db.inventory.find( { status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ] } )

What is the command to check whether you are on the master server or not.

db.isMaster()

How would you remove the property "points" from the object {_id: 7, name: Susan, points: 88} in the people collection?

db.people.update({_id: 7}, {$unset: {points: 1"}}, {mullti: true});

If you want to replace a person object with _id: 56 if it already exists, but insert instead if there is already a person with _id: 56 in the collection in a single MongoDB operation, how would you write this?

db.people.updateOne({_id: 56}, {name: "Joe"},{upsert: true})

In Mongo Shell, how would you add a new column 'status' containing a default value of 'new' to an existing document {_id: 7, name: "Susan"} in a people collection?

db.people.updateOne({_id: 7}, {$set: {status: "new"}});

How would you remove the property "points" from all objects in the people collection?

db.people.updateOne({_id: 7}, {$unset: {points: 1"}});

What is difference between dependencies and devDependencies?

dependencies are modules your project depends on. devDependencies are modules you use to develop your project. Examples of dependencies are request, through2 and concat-stream. Examples of devDependencies are grunt, mocha, eslint, tape, and browserify.

Should I start out with sharded or with a non-sharded MongoDB environment?

starting unsharded for simplicity and quick startup unless your initial data set will not fit on single servers. Upgrading to sharding from unsharded is easy and seamless, so there is not a lot of advantage to setting up sharding before your data set is large.

What are the differences between updateOne and replaceOne?

updateOne will only modify parts of the document, as specified in the update modifier, while replaceOne, will replace the first matching document with the supplied document.

Write the command to insert a document in a database called school and collection called persons (using the MongoDB shell).

use school; ex: db.persons.insert( { name: "kadhir", dept: "CSE" } )


Conjuntos de estudio relacionados

Costs and Benefits of Government

View Set

EMT-B Chapter 28 Abdominal and Genitourinary Injuries

View Set

A & P 1 chapter 11 ( Wiley plus )

View Set

Cell Theory, Prokaryotes, And Eukaryotes

View Set

Pairing of Shoulder Girdle and Shoulder Joint Movements

View Set

Unit Two- Career Portfolio and Resume

View Set

AWS Certified Cloud Practitioner Test

View Set