- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1659 Articles for Big Data Analytics
120 Views
To query MongoDB with length criteria, you can use regex. Following is the syntaxdb.yourCollectionName.find({ ‘yourFieldName’: { $regex: /^.{yourLengthValue1, yourLengthValue2}$/ } });Let us create a collection with documents. Following is the query> db.queryLengthDemo.insertOne({"StudentFullName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01ae353decbc2fc927c0") } > db.queryLengthDemo.insertOne({"StudentFullName":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01b4353decbc2fc927c1") } > db.queryLengthDemo.insertOne({"StudentFullName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01c2353decbc2fc927c2") } > db.queryLengthDemo.insertOne({"StudentFullName":"Robert Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01e2353decbc2fc927c3") } > db.queryLengthDemo.insertOne({"StudentFullName":"Chris Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01f1353decbc2fc927c4") }Following is the query to display ... Read More
324 Views
To iterate over all MongoDB databases, you need to switch your database to admin. Following is the query to switch to admin and get information about all the databases> switchDatabaseAdmin = db.getSiblingDB("admin"); admin > allDatabaseName = switchDatabaseAdmin.runCommand({ "listDatabases": 1 }).databases;This will produce the following output[ { "name" : "admin", "sizeOnDisk" : 495616, "empty" : false }, { "name" : "config", "sizeOnDisk" : 98304, "empty" : false }, { "name" : "local", "sizeOnDisk" : 73728, "empty" : false }, { "name" : "sample", "sizeOnDisk" : 1335296, "empty" : false }, { "name" : "sampleDemo", "sizeOnDisk" : 278528, "empty" : false }, { "name" : "studentSearch", "sizeOnDisk" : 262144, "empty" : false }, { "name" : "test", "sizeOnDisk" : 8724480, "empty" : false } ]
509 Views
You need to use insert() for this. Whenever you insert custom _id values and the document already exist with the custom _id value then an error is visible. Let us first create a collection with documents. Under this, we tried adding the same document again and this resulted in an error> db.customIdDemo.insert({"_id":1, "StudentName":"John"}); WriteResult({ "nInserted" : 1 }) > db.customIdDemo.insert({"_id":1, "StudentName":"Carol"}); WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error collection: admin.customIdDemo index: _id_ dup key: { : 1.0 }" } }) > db.customIdDemo.insert({"_id":2, "StudentName":"Carol"}); ... Read More
96 Views
You can use buidInfo along with runCommand to check MongoDB for 32 bits or 64 bits. First switch your database to admin. Following is the syntaxuse adminAfter that use the following syntax to know if my server runs MongoDB 64 bits or notdb.runCommand(buildInfo)Now execute the above syntax> use admin switched to db admin > db.runCommand("buildInfo");Following is the output displaying MongoDB is 64 bits{ "version" : "4.0.5", "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412", "targetMinOS" : "Windows 7/Windows Server 2008 R2", "modules" : [ ], "allocator" : "tcmalloc", "javascriptEngine" : "mozjs", "sysInfo" : "deprecated", "versionArray" : [ ... Read More
606 Views
Access objects using dot notation. Let us first create a collection with documents> db.nestedObjectDemo.insertOne({"Student" : { "StudentDetails" : { "StudentPersonalDetails" : { "StudentName" : [ "John" ], ... "StudentCountryName" : [ "US" ], ... "StudentCoreSubject" : [ "C", "Java" ], ... "StudentProject" : [ "Online Book Store", "Pig Dice Game" ] } } } }); { "acknowledged" : true, "insertedId" : ObjectId("5c99dfc2863d6ffd454bb650") }Following is the query to display all documents from a collection with the help of find() method> db.nestedObjectDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c99dfc2863d6ffd454bb650"), "Student" : { "StudentDetails" : ... Read More
521 Views
To search multiple fields for multiple values in MongoDB, you can use $text and $search operator. Let us first create a collection with documents>db.searchMultipleFieldsDemo.insertOne({"_id":100, "FirstSubject":"Java", "SecondSubject":"MongoDB"}); { "acknowledged" : true, "insertedId" : 100 } >db.searchMultipleFieldsDemo.insertOne({"_id":101, "FirstSubject":"MongoDB", "SecondSubject":"MySQL"}); { "acknowledged" : true, "insertedId" : 101 } >db.searchMultipleFieldsDemo.insertOne({"_id":102, "FirstSubject":"MySQL", "SecondSubject":"Java"}); { "acknowledged" : true, "insertedId" : 102 }Following is the query to display all documents from a collection with the help of find() method> db.searchMultipleFieldsDemo.find().pretty();This will produce the following output{ "_id" : 100, "FirstSubject" : "Java", "SecondSubject" : "MongoDB" } { "_id" : 101, "FirstSubject" : "MongoDB", "SecondSubject" : "MySQL" } { ... Read More
768 Views
Use aggregate() method to get all the non-distinct values of a field. Let us first create a collection with documents> db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5c995078863d6ffd454bb647") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c995081863d6ffd454bb648") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c995089863d6ffd454bb649") } > db.findAllNonDistinctDemo.insertOne({"UserName":"David", "UserAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c995093863d6ffd454bb64a") } > db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c99509d863d6ffd454bb64b") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert", "UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9950a7863d6ffd454bb64c") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert", "UserAge":25}); ... Read More
123 Views
To create an index in MongoDB, use the ensureIndex() method. Let us first create a collection using following query> db.createCollection(&qu/ot;creatingUniqueIndexDemo"); { "ok" : 1 }Following is the query to create an index on the above collection:> db.creatingUniqueIndexDemo.ensureIndex({"UserCountryName":1}, {unique:true}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Following is the query to insert some documents in the above collection>db.creatingUniqueIndexDemo.insertOne({"UserName":"John", "UserAge":21, "UserCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9947bd330fd0aa0d2fe4d8") } >db.creatingUniqueIndexDemo.insertOne({"UserName":"Mike", "UserAge":23, "UserCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9947c9330fd0aa0d2fe4d9") } >db.creatingUniqueIndexDemo.insertOne({"UserName":"Robert", "UserAge":26, "UserCountryName":"US"}); 2019-03-26T02:57:52.670+0530 E QUERY [js] WriteError: ... Read More
190 Views
Following is the syntax to drop all indexes from all collections in a MongoDB database using command linedb.getCollectionNames().forEach(function(yourVariableName) { db.runCommand({dropIndexes: yourVariableName, index: "*"}); });The above syntax will drop all indexes except _id.Let us check the current database. Following is the query> dbThis will produce the following outputTestFollowing is the query to let us show some indexes from a collection before dropping indexes> db.indexingDemo.getIndexes();This will produce the following output[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", ... Read More
68 Views
To get all the MongoDB documents with some given criteria’s, following any of the below given casesCase 1 Following is the query to get all the documents without a single criterion using $ne operatordb.yourCollectionName.find({yourFieldName:{$ne:"yourValue"}}).pretty();Case 2 Following is the query to get all the documents without two given criterions using $nin operatordb.yourCollectionName.find({yourFieldName:{$nin:["yourValue1", "yourValue2"]}}).pretty();Let us first create a collection. Following is the query to create a collection with documents>db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Larry", "StudentSubjectName":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d82330fd0aa0d2fe4d2") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Chris", "StudentSubjectName":"C++"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d8f330fd0aa0d2fe4d3") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Robert", "StudentSubjectName":"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d99330fd0aa0d2fe4d4") } ... Read More