InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 51. |
Adding an index can potentially decrease the speed of which of the following operations? |
|
Answer» YES, there is very MUCH a simpler way of achieving this without having to do this programmatically. The $unwind operator deconstructs an array field resulting in a document for each ELEMENT. Consider user “John” with multiple addresses { "_id" : 1, "name" : "John", addresses: [ "Permanent Addr", "TEMPORARY Addr", "Office Addr"] } db.users.aggregate( [ { $unwind : "$addresses" } ] )would result in 3 documents, one for each of the addresses { "_id" : 1, " name " : " John ", " addresses " : "Permanent Addr" } { "_id" : 1, " name " : " John ", " addresses " : "Temporary Addr" } { "_id" : 1, " name " : " John ", " addresses " : "Office Addr" } |
|
| 52. |
How would you troubleshoot locking issues in MongoDB? |
|
Answer» The $GRAPHLOOKUP LOOKS like this for a query from the EMPLOYEES collection where “manager” is the self-referencing field db.employees.aggregate( [ { $graphLookup: { from: "employees", startWith: "DAVID", connectFromField: "manager", connectToField: "name", as: "REPORTING Structure" } } ] )The value of as, which is “Reporting Structure” in this case is the name of the array field which contains the documents traversed in the $graphLookup to reach the output document. For the following documents in the employee collection, { "_id" : 4, "name" : " David " , "manager" : "Sarah" } { "_id" : 5, "name" : "John" , "manager" : "David" } { "_id" : 6, "name" : "Richard", "manager" : " John " } { "_id" : 7, "name" : "Stacy" , "manager" : " Richard " }“Reporting Structure” for each output document would look like this { "_id" : 5, "name" : "John" , "manager" : "David", "Reporting Structure" : [] } { "_id" : 6, "name" : "Richard", "manager" : " John ", "Reporting Structure" : [{ "_id" : 5, "name" : "John" , "manager" : "David" }] } { "_id" : 7, "name" : "Stacy" , "manager" : " Richard", "Reporting Structure" : [{ "_id" : 5, "name" : "John", "manager" : "David" } { "_id" : 6, "name" : "Richard", "manager" : " John " }] } |
|
| 53. |
How does MongoDB WiredTiger storage engine ensure data durability? |
|
Answer» Recursive queries can be performed within a collection using $graphLookUp which is an aggregate pipeline stage. If a collection has a self-referencing field like the classic example of MANAGER for an employee, then a QUERY to get the entire REPORTING structure for manager “David” would look like this db.employees.aggregate( [ { $graphLookup: { from: "employees", startWith: "David", connectFromField: "manager", connectToField: "name", as: "Reporting Structure" } } ] )For the following documents in the employee collection, { "_id" : 4, "name" : " David " , "manager" : "Sarah" } { "_id" : 5, "name" : "John" , "manager" : "David" } { "_id" : 6, "name" : "Richard", "manager" : " John " } { "_id" : 7, "name" : "Stacy" , "manager" : " Richard " }Output of the above $graphLookup operation would result in the following 3 documents returned { "_id" : 5, "name" : "John" , "manager" : "David", … } { "_id" : 6, "name" : "Richard", "manager" : " John ", … } { "_id" : 7, "name" : "Stacy" , "manager" : " Richard", … }The hierarchy starts with “David” which is specified in startWith and there on the DATA for each of the members in that reporting hierarchy are fetched recursively |
|
| 54. |
What level of concurrency does MongoDB has? |
|
Answer» MongoDB has the db.collection.explain(), cursor.explain() and explain command to provide INFORMATION on the query PLAN. The results of explain contain a lot of information, key ones being
|
|
| 55. |
What are the different storage engine options available for MongoDB? |
|
Answer» When "FAST reads" are the single most important criteria, EMBEDDED documents can be the best way to model one-to-one and one-to-many relationships. Consider the example of certifications awarded to an employee, in the below example the certification data is embedded in the employee document which is a denormalized way of storing data { _id: "10", name: "Sarah Jones", certifications: [ { certification: "Certified Project Management Professional”, certifying_auth: “PMI”, DATE: "06/06/2015" }, { certification: "Oracle Certified Professional”, certifying_auth: “Oracle Corporation”, date: "10/10/2017" } ] }In a normalized form, there would be a reference to the employee document from the certificate document, example { employee_id: "10", certification: "Certified Project Management PROFESIONAL”, certifying_auth: “PMI”, date: "06/06/2015" }Embedded documents are best used when the entire relationship data needs to be frequently retrieved together. Data can be retrieved via single query and hence is much faster. Note: Embedded documents should not grow unbounded, otherwise it can slow down both read and write operations. Other factors like consistency and frequency of data change should be considered before making the final design decision for the application. |
|
| 56. |
In a 3 Member Replica set if the secondary is lagging behind what can be checked for same? |
|
Answer» The $addToSet operator should be used with the $each modifier for this. The $each modifier allows the $addToSet operator to add multiple values to the array field. Example, start ups are tagged as per the technology skill that they excel in { _id: 5, name: "XYZ Technology", SKILLS: [ "BIG DATA", "AI", “Cloud” ] }Now the start up needs to be updated with additional skills db.startups.update( { _id: 5 }, { $addToSet: { skills: { $each: [ "MACHINE Learning", "RPA" ] } } } )The resultant document after update() { _id: 5, name: "XYZ Technology", skills: [ "Big Data", "AI", “Cloud”, "Machine Learning", "RPA"] }Note: There is no particular ORDERING of elements in the modified set, $addToSet does not guarantee that. Duplicate items will not be added. |
|
| 57. |
What authorization model does MongoDB follow? |
Answer»
Consider the following compound index { "accountHolder": 1, "accountNumber": 1, "currency": 1 }The index prefixes are { accountHolder: 1 } { accountHolder: 1, accountNumber: 1 }Query plan will USE this index if the query has the following fields
|
|
| 58. |
What is localhost exception in MongoDB? |
|
Answer» MongoDB provides database profiler which captures and stores detailed information about commands executed on running instance. Captured details INCLUDE CRUD operations, administrative commands and configuration commands. The data collected by the profiler is stored in the system.profile collection in the admin database. By default, the profile is turned off. We can enable it and set to different profiling levels based on the requirements. It provides 3 profiling levels:
To capture the SLOW running queries, we can start the profiler with profiling level 1 or 2 and Default slow operation threshold is 100 milliseconds. We can CHANGE this threshold by specifying a slowms option. Eg: To enable profiler which captures all queries slower than 50ms below command should be used: db.setProfilingLevel(1, { slowms: 50 }) |
|
| 59. |
db.sample.find( { b : 1 } ).sort( { c : 1, a : 1 } ) db.sample.find( { c : 1 } ).sort( { a : 1, b : 1 } ) db.sample.find( { a : 1 } ).sort( { b : 1, c : 1 } ) |
|
Answer» When we create any collection in MongoDB, a UNIQUE index on the _id field is created automatically. This unique index prevents applications from inserting multiple same DOCUMENTS with the same value for the _id field. This is forced by the system and we cannot drop this index on the _id field. Moreover, in replica sets the unique _id values are used in the oplog to reference documents to update. In a sharded CLUSTER if we do not have unique _id values across the sharded collection chunk migrations MAY fail as when documents migrate to another shard, any identical values will not be inserted to receiver shard. In such cases, we should code application such that it ensures uniqueness on _id for given collection across all shards in a sharded cluster. If we use _id as the shard key, this uniqueness of values will automatically be forced by the system. In such case chunk ranges will be assigned to SINGLE shard and then the shard will force uniqueness on the values in that range. |
|
| 60. |
Which of the following queries will require that to load every document into RAM in order to fulfil the query? |
|
Answer» In sharded cluster we may have a database which has sharded as well as non sharded collections. While the sharded collections are spread across all the shards, all the unsharded collections are stored on a single shard known as a primary shard. Every database in the sharded collection has its own primary shard. When we create any new database, mongos pick the shard with the least amount of data in cluster and marks it as a primary shard. If there is a need to change the primary shard we can do so by using the movePrimary command. This migration may take significant time to complete. We should not access any collections associated with MIGRATING database until the process completes. Also, the migration of primary shard should be done at the LEAN time as it may impact the performance of the OVERALL cluster. Eg. To MIGRATE the primary shard of accounts database to Shard0007 below command should be used. db.adminCommand( { MOVEPRIMARY : "accounts", to : "shard0007" } ) |
|
| 61. |
"_id" : 1 "a" : 1 "c" : 1, "b" : 1, "a" : 1 |
|
Answer» MongoDB provides several utilities for data movement activities like mongodump, mongoexport ETC. Mongodump is used to export the contents of the collection in an external file in the BSON(binary) format. The contents exported by this method can then be used by mongorestore command to restore in another database or different collection. Mongodump does not capture index data and only captures the data present in the backup. Since the contents are exported in binary format using this method we cannot use it for exporting to CSV file. To export the contents in JSON or CSV format we can use the mongoexport command. The exported collection can then be RESTORED using mongoimport command. Since Mongoexport exports cannot export in BSON all the rich BSON data TYPES are not preserved while exporting the data. Due to this reason, mongoexport should be used with CAREFUL consideration. Below is the command that can be used for the same. mongoexport --host host:27017 -d test -C sample --type=csv -f fields -o sample.csv |
|
| 62. |
Suppose we have the following indexes on the sample collection: |
|
Answer» Indexes are important to consider while designing the databases as they impact the performance of the APPLICATIONS. Without index, query will perform collection scan in which all the documents of the collection are scanned one by one to find the matching fields of the query. If we have indexed for a particular query, MongoDB can use the index to limit the number of documents scanned to execute the query as the indexes store the values of the query in ascending or descending ordered form. While the indexes help in improving the performance of find operations for write operations like insert and update there can be a SIGNIFICANT NEGATIVE impact of adding indexes as with modifications with each write MongoDB WOULD need to update the indexes associated with the index also. This would be overhead on the system and we may end up with performance degradation. So while the find() will improve performance the operators updateOne and insertOne would degrade the performance as with every update or insert related indexes would need to be updated. |
|
| 63. |
What are the different situations when we should consider sharding over replication? |
|
Answer» In MongoDB data set consistency is ensured by locking. In any database system long running queries DEGRADE the performance as requests and operations have to wait for a lock. Locking issues are INTERMITTENT and so need to be resolved immediately. MongoDB provides US with tools and utilities to troubleshoot these locking issues. The serverStatus() command provides us a view of the system including the locking-related information. We should look for locks and a globalLock section of serverStatus() command for TROUBLESHOOTING locking issues. We can use below commands to filter locking related information from the serverStatus output. db.serverStatus().globalLock db.serverStatus().locksTo get the approximate average wait time for a lock mode we can divide locks. timeAcquiringMicros by locks.acquireWaitCount. To check the number of times deadlocks occurred locks.deadlockCount should be checked. If the application performance is constantly degraded there might be concurrency issues, in such cases, we should look at globalLock.currentQueue.total. A high value INDICATES concurrency issues. Sometimes globalLock.totalTime is high relative to uptime which suggests database has been in a lock state for a significant time. |
|
| 64. |
What are indivisible/jumbo chunks? |
|
Answer» MONGODB WiredTiger ensures data durability with journaling and checkpoints. Journals are write-ahead logs which checkpoints are point-in-time snapshots.
In wiredTiger, with the start of each operation, a point-in-time snapshot is taken which presents a consistent view of in-memory data. WiredTiger then writes all snapshot data to the disk in a consistent way across all data files. This data on disk is durable and acts as a checkpoint in the data files. The checkpoint ensures all data files are consistent from the LAST checkpoint. These checkpoints usually OCCUR every 60SEC so we have a consistent snapshot every 60sec of interval thus ensuring durability.
Journal is write-ahead logs which persist all data changes between 2 checkpoints. In case we require data between checkpoints for recovery these journal files can be used. These general files act as crash recovery files in case of interrupts. Once the system is BACK up these journal files can be replayed for recovery. |
|
| 65. |
When should data for a sharded cluster be pre-split? |
|
Answer» Concurrency in MongoDB is different for different storage engines. While WiredTiger uses document level concurrency control for write operations, MMAPV1 has concurrency at the collection level. In WiredTiger locking is at document level due to which multiple clients can MODIFY documents at the same TIME and so uses optimistic concurrency control for most read and writes. Instead of exclusive locks, it uses only intent locks at the global, database and collection levels. In case the conflict is detected by storage engine between operations, one will incur a write conflict causing MongoDB to transparently retry that operation. SOMETIMES global “instance-wide” locks are required for global operations which involve multiple databases. Exclusive database lock incurs for operations such as DROPPING a collection. MMAPv1 still uses collection level lock meaning if we have 3 applications running against the same collection, 2 would have to wait before FIRST application completes its own as it applies a collection level write lock. |
|
| 66. |
{ d : 1, a: 1 , b:1} { p : 1, q : 1, r : 1, s : 1, t : 1 } { x : 1 } { c : 1, y : 1 } { c : 1, d : 1 } |
|
Answer» The storage engine is the component that lies between the database and storage layer and is primarily responsible for managing data. MongoDB PROVIDES few choices of storage engines, enabling us to use best suited for our applications. Choosing the appropriate storage engine can significantly impact performance.
WiredTiger replaced MMAPV1 in 3.2 to become default storage engine. If we install MongoDB and do not specify any storage engine, wiredTiger is enabled. As it provides a document-level concurrency model, checkpointing, and compression it is suited for most WORKLOADS. It also supports encryption at rest in MongoDB Enterprise.
Various applications REQUIRE predictable latencies which can be achieved by storing the documents in memory rather than on disk. In-Memory Storage Engine is helpful for such applications. It is available only in MongoDB enterprise edition.
MongoDB started with MMAPv1 storage engine only but it was successful for a specific SUBSET of use cases only due to which it is deprecated from version 4.0. |
|
| 67. |
Shard key options: |
|
Answer» If secondaries are FALLING behind they are experiencing REPLICATION lag, which is a delay in the application of oplog from primary to secondary. Replication lag can be a significant issue and can seriously affect MongoDB replica set deployments. Excessive replication lag makes a member ineligible to quickly become primary and increases the possibility of distributed read operations to be inconsistent. We can check the replication lag by calling the rs.printSlaveReplicationInfo() method or by running the rs.status() command. Possible causes of replication lag include:
|
|
| 68. |
90% db.emploqees.find( { a : 1, b : 1, c : 1, d : 1 }, { p : 1, q : 1, r: 1, s : 1, t : 1 } ) 5% db.emploqees.updateOne( { x: 1 }, { $set : { r : 1, q : 1, t : 1, s : 1 } } ) 1% db.emploqees.updateOne( { x: 1 }, { $set : { c : 1, y: 1 } } ) |
|
Answer» MongoDB follows Role access control authorization model(RBAC). In this model, USERS are assigned one or more roles which provide them access to the database resources and operations. Apart from these role assignments users do not have any access to the resources. When we enable internal authentication it automatically enables client authorization. The authorization can be enabled by starting mongod with –auth OPTION or providing security.authorization setting in the config file.
These group of privileges are roles that can be assigned to the user. MongoDB provides several Build-in roles like Database User Roles, Database Administration Roles, Cluster Administration Roles, Backup and Restoration Roles, All-Database Roles, Superuser Roles But we can also create our own custom roles based on the requirement which are called User-defined roles. For Example: Suppose we WANT to create a role to MANAGE all operations in the cluster we can create below user-defined role. use admin db.createRole( role: "mongostatRole", privileges: [ { resource: { cluster: true }, actions: [ "serverStatus" ] } ], roles: [] ) |
|
| 69. |
Based on the below workload for a production system which of the shard key option is best suited? |
|
Answer» If we start Mongo server using mongod with the --auth option it will enable authorization but we will not be able to do anything, even listing databases using SHOW DBS will fail with an error "not authorized on admin to execute the command". This is because we have not authenticated yet to the database. But this is a NEW database with no users in this database, so how can we create new users with no authorization. MongoDB provides localhost exception for creating the first user on the database without any authorization. With this first user, we can create other users with relevant access. But there are a few considerations to it: |
|
| 70. |
What are different read preference modes for reading from MongoDB? |
|
Answer» Loading EVERY document into RAM means that QUERY will not be using index efficiently and will have to fetch documents from disk to ram. For using an index, the initial match in the find statement should either use index or index PREFIX. Below query has b key to find that does not use any existing index or index prefix so it would have to fetch from disk. db.sample.find( { b : 1 } ).SORT( { c : 1, a : 1 } ) |
|
| 71. |
Why are MongoDB Replica set operations considered idempotent? |
|
Answer» In replication we have multiple copies of the same data in sync with each other. It is mainly useful for High availability purpose. While in sharding we divide our entire dataset in small chunks and distribute among several servers. Sharding is used where we have some SORT of bottleneck in terms of hardware or for getting the benefits of query parallelism. If our dataset is very small sharding would not provide many advantages but as the data grows we should MOVE to sharding. Below are a few of the SITUATIONS where sharding is recommended over replication.
By breaking the dataset over shards will mean having more RESOURCES available to handle the subset of data it owns, and operations of moving data ACROSS machines for replication, backups, restores will also be faster. |
|
| 72. |
db.sample.deleteOne( { state : "WA" } ) |
|
Answer» In some cases, chunks can grow beyond the specified chunk size but cannot undergo a split. The most COMMON scenario is when a chunk represents a single shard key value. Since the chunk cannot split, it continues to grow beyond the chunk size, becoming a jumbo chunk. These jumbo chunks can become a PERFORMANCE bottleneck as they continue to grow, ESPECIALLY if the shard key value occurs with high frequency. The addition of new data or new shards can result in data distribution imbalances WITHIN the CLUSTER. A particular shard may acquire more chunks than another shard, or the size of a chunk may grow beyond the configured maximum chunk size. MongoDB ensures a balanced cluster using two processes: chunk splitting and the balancer. |
|
| 73. |
How many oplog entries will be created when the below query is run on the primary. |
|
Answer» Chunk split operations are carried out automatically by the system when any insert operation causes chunk to exceed the maximum chunk size. Balancer then migrates RECENTLY split chunks to new shards. But in some cases we may want to pre-split the chunks manually:
To split the chunks manually we can use the split command with helper sh.splitFind() and sh.splitAt(). Example: To split the chunk of employee collection for employee id field at a value of 713626 below command should be used. sh.splitAt( "test.people", { "employeid": "713626" } )We should be CAREFUL while pre-splitting chunks as sometimes it can lead to a collection with different sized chunks. |
|
| 74. |
{"firstName" : "Arthur", "lastName" : "Aaronson", "state" : "WA", "city" : "Seattle", "likes" : [ "dogs", "cats" ] } {"firstName" : "Beth", "lastName" : "Barnes", "state" : "WA", "city" : "Richland", "likes" : [ "forest", "cats" ] } {"firstName" : "Charlie", "lastName" : "Carlson", "state" : "CA", "city" : "San Diego", "likes" : [ "desert", "dogs" ] } {"firstName" : "Dawn", "lastName" : "Davis", "state" : "WA", "city" : "Seattle", "likes" : [ "forest", "mountains" ] } |
|
Answer» Shard key selection is based on the workload. Since the first query is being USED 90% it should be driving the selection for selection of shard key. Combination of FIELDS from that query would make the best shard key. This ELIMINATES option b, c and d. Option a and e uses a subset of fields from the most used workload and both can be shard key but option has more fields and so would be more suitable. |
|
| 75. |
Consider sample collection with below entries for a 5-member replica set. |
|
Answer» In MONGODB we can read from Primary as well as secondary members of the replica set. This behaviour can be CONTROLLED by us as we can define the desired Read preference to which clients route read operations to a member of the replica set. If we do not SPECIFY any real preference by default MongoDB will read from primary. There are situations when you would want to reduce the load on your primary by forcing APPLICATIONS to read from secondary. Below are different MongoDB read preference modes:
This is the default mode. Applications read from the replica set primary.
In this mode, all applications read from primary but if the primary member is not available they start reading from secondary.
All applications read from the secondary members of the replica set.
In this mode, all applications read from secondary but if any secondary member is not available they start reading from primary. In this mode, applications read from the member which is nearest to them in terms of network latency, irrespective of the member being primary or secondary. |
|
| 76. |
How can we change the size of oplog? |
|
Answer» Idempotence is the property of certain operations whereby they can be applied multiple times WITHOUT changing the result beyond the initial application. In MongoDB, oplog is idempotent meaning even if they are applied multiple times the same output will be produced. So if the server goes down and we need to apply oplogs there WOULD not be any inconsistency, even if it applies any logs that were ALREADY applied there will not be CHANGED in the database end state. Also, there was a desire to have a new state of a document to be independent of the previous state. For these all operators that rely on the previous state to determine new value needed to be TRANSFORMED to see the actual values. For example, if an addition operation results in modifying the value from ‘21’ to ’30’, the operation should be changed to set value ‘30’ on the field. Replaying the operator multiple times should produce the same result. |
|
| 77. |
How is data synchronized from primary to other members in MongoDB replica sets? |
|
Answer» MongoDB applies database operations on the PRIMARY and then records the operations on the primary’s oplog. The secondary members then copy and apply these operations in an asynchronous process. For each operation, there is separate oplog entry. First, let’s check how many ROWS the query WOULD fetch by changing delete to find operation. db.sample.find( { state : "WA" } ) This will give all the rows with the state is WA. {"firstName" : "Arthur", "lastName" : "Aaronson", "state" : "WA", "city" : "SEATTLE", "likes" : [ "dogs", "cats" ] } {"firstName" : "Beth", "lastName" : "Barnes", "state" : "WA", "city" : "Richland", "likes" : [ "forest", "cats" ] } {"firstName" : "Dawn", "lastName" : "Davis", "state" : "WA", "city" : "Seattle", "likes" : [ "forest", "mountains" ] }Now Ideally delete should remove all matching rows but query says deleteOne. If the query would have said deleteMany then all the matching rows would have been deleted and there would be 3 oplog ENTRIES but deleteOne will remove first matching row. So 1 oplog entry will be generated with provided query |
|
| 78. |
{ "_id" : 1, "x" : 1, "y" : 2 }{ "_id" : 2, "x" : 2, "y" : 4 }{ "_id" : 3, "x" : 3, "y" : 7 }{ "_id" : 4, "x" : 4, "y" : 10 }{ "_id" : 5, "x" : 5, "y" : 75 } |
|
Answer» The oplog is operation logs that keep an update of all operations that modify the data stored in databases. We can define the oplog size while starting MONGODB by specifying the --oplog option. If we do not specify this option it will take the default values which is 5% of physical memory in case of wiredTiger. While the default value is sufficient for most workloads in some cases we may need to change the oplog size for the replica set. OPlog size is CHANGED in a ROLLING manner, first, we change on all secondary and then a primary member of the replica set. To change oplog size
use local db.oplog.rs.stats().maxSize
|
|
| 79. |
db.sample.updateMany( { y : { $gte : 10 } },{ $set : { y : 75 } } ) |
|
Answer» Every operation on the primary is logged in operation logs known as oplog. These oplogs are replicated to For a healthy replica set system, it is RECOMMENDED that all members are in sync with no replication lag. Data is first written on primary by the applications then replicated to secondary. This synchronization is important to MAINTAIN up-to-date copies of data on all members. Synchronization happens in 2 ways: initial sync and continuous replication.
|
|
| 80. |
Which of the below documents will be modified using the query provided. |
|
Answer» The FIRST part of the query WOULD give all documents where y>=10. So we will have 2 documents i.e d> { "_id" : 4, "x" : 4, "y" : 10 } e> { "_id" : 5, "x" : 5, "y" : 75 }Now the SECOND part of the query would update the value of Y for above 2 documents to 75, but we already have a document with value y:75, that will not be updated. Finally, we will have one 1 document that will be updated by the provided query. d> { "_id" : 4, "x" : 4, "y" : 10 } |
|
| 81. |
What is the notion of schema-less in MongoDB? |
|
Answer» In MONGODB data is stored as JSON documents. These documents can have different sets of fields, with different data type for each field. For example, we can have a collection with number, varchar and array all as different documents. { “a” : 143 } { “name” : “john” } { “x” : [1,2,3] }It is not correct to say MongoDB is schemaless, in fact, schema plays an important role in the designing of MongoDB applications. MongoDB has a dynamic schema having database structure with collections and indexes. These collections can be created either implicitly or explicitly. Due to the dynamic behaviour of the schema, MongoDB has several advantages over RDBMS systems. Schema Migrations BECOME very easy as in traditional systems we had to use ALTER TABLE command after adding any column which could result in downtime. In MongoDB, such adjustments become transparent and automatic. For example, if we want to add CITY field to PEOPLE collection, we can add the attribute and resave, that’s it. Whereas in a traditional system we would have to run ALTER TABLE command FOLLOWED by REORG which would require downtime. |
|
| 82. |
Being a member of MongoDB production support team you are asked to create a custom role that will be assigned to the monitoring team to run mongostat. How would you do so? |
|
Answer» In MongoDB we have Built-in ROLES as well as custom roles. Built-in roles already have pre-defined access associated with them. We can assign these roles directly to users or groups for access. To run MONGOSTAT we WOULD require access to run the server status on the server. Built-in role cluster monitor comes with required access for the same. Custom roles or user-defined roles are the ones where we have to manually define access ACTIONS to a particular resource. MongoDB provides method db.createRole() for creating user-defined roles. These roles can be created in a specific database as MongoDB uses a combination of database and role name to uniquely define the role. We will create a custom role mongostatRole that provides only the privileges to run mongostat. First, we need to connect to mongod or mongos to the admin database with a user that has privileges to create roles in the admin as well as other databases. mongo --port 27017 -u admin -p 'abc***' --authenticationDatabase 'admin'Now we will create a desired custom role in the admin database. use admin db.createRole( role: "mongostatRole", privileges: [ {resource: { cluster: true }, actions: [ "serverStatus" ] } ], roles: [] )This role can now be assigned to members of monitoring team. |
|
| 83. |
Unstructured data means that within a collection there is the possibility that some fields may not exist for all the documents. How can documents be queried solely based on field availability? |
|
Answer» only those DOCUMENTS that contain the field specified in the query. For the FOLLOWING documents in EMPLOYEE COLLECTION { _id: 1, name: "Jonas", linkedInProfile: null }, { _id: 2, name: “WILLIAMS” } The query { linkedInProfile: { $exists: true } } will return only the employee “Jonas” |
|
| 84. |
Databases like Oracle use the “IS NULL” operator to retrieve only the records where the queried value is actually NULL (and not missing), how can the same be achieved in MongoDB? |
|
Answer» This can be achieved in MongoDB using the $type operator. A null value, i.e., BSON type null has the type number 10. Using this type number, only those documents can be retrieved whose value is null. Take the example of the below two documents in STARTUP collection { _id: 1, name: "XYZ Tech", website: null }, { _id: 2, name: “ABC Pvt Ltd” } The query { website : { $type: 10 } } will retrieve only those documents where the website is null, in the above case it WOULD be the startup “XYZ Tech” NOTE: The query { website : null } on the other HAND will match documents where the website is null or the documents where the website field does not EXIST. For the above collection data, this query will return both the startups. |
|
| 85. |
Pagination can be achieved in MongoDB using combination of limit() and skip() |
|
Answer»
|
||