- 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
2K+ Views
To insert a document with date in MongoDB, use Date(). Following is the syntax“yourFieldName”:new Date(yourDateValue);Let us create a collection with documents. Following is the query>db.insertDocumentWithDateDemo.insertOne({"UserName":"Larry", "UserMessage":"Hi", "UserMessagePostDate":new Date("2012-09-24")}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cca58a629b87623db1b16") } >db.insertDocumentWithDateDemo.insertOne({"UserName":"Chris", "UserMessage":"Hello", "UserMessagePostDate":new Date("2015-12-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cca71a629b87623db1b17") } >db.insertDocumentWithDateDemo.insertOne({"UserName":"Robert", "UserMessage":"Bye", "UserMessagePostDate":new Date("2019-01-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cca85a629b87623db1b18") }Following is the query to display all documents from a collection with the help of find() method> db.insertDocumentWithDateDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9cca58a629b87623db1b16"), "UserName" : "Larry", "UserMessage" : "Hi", ... Read More
153 Views
In order to connect to my table by command line, you need to use db commanddb.yourCollectionName.find();Let’s say we have a database “sample” with some collections. First check the current database> use sample; switched to db sample > db; Sample Now we have reached the database sample. The database “sample” is having the following collections: > show collections;This will produce the following outputarraySizeErrorDemo basicInformationDemo copyThisCollectionToSampleDatabaseDemo deleteAllRecordsDemo deleteDocuments deleteDocumentsDemo deleteSomeInformation documentWithAParticularFieldValueDemo employee findListOfIdsDemo findSubstring getAllRecordsFromSourceCollectionDemo getElementWithMaxIdDemo internalArraySizeDemo largestDocumentDemo makingStudentInformationClone oppositeAddToSetDemo prettyDemo returnOnlyUniqueValuesDemo selectWhereInDemo sourceCollection studentInformation sumOfValueDemo sumTwoFieldsDemo truncateDemo updateInformation userInformationHere is the correct way of connecting to a table i.e. collection. You ... Read More
318 Views
In order to handle empty data, you can use $ne operator. Let us create a collection with documents. Following is the query>db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John", "StudentCountryName":""}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cbd5ca629b87623db1b12") } >db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John", "StudentCountryName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cbd6ba629b87623db1b13") } > db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cbd71a629b87623db1b14") }Following is the query to display all documents from a collection with the help of find() method> db.handlingAndEmptyDataDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9cbd5ca629b87623db1b12"), "StudentName" : "John", "StudentCountryName" : "" } { "_id" : ObjectId("5c9cbd6ba629b87623db1b13"), "StudentName" : ... Read More
154 Views
Use use renameCollection() to change collection name in MongoDB. Following is the syntaxdb.yourOldCollectionName.renameCollection("yourNewCollectionName");Let us create a collection with documents. Following is the query> db.savingInformationDemo.insertOne({"StudentName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cb44da629b87623db1b07") } > db.savingInformationDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cb45da629b87623db1b08") } > db.savingInformationDemo.insertOne({"StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cb461a629b87623db1b09") } > db.savingInformationDemo.insertOne({"StudentName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cb465a629b87623db1b0a") }Following is the query to display all documents from a collection with the help of find() method> db.savingInformationDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9cb44da629b87623db1b07"), "StudentName" : "Larry" } { ... Read More
1K+ Views
Following is the syntax to loop through collections with cursorvar anyVariableName1; var anyVariableName2= db.yourCollectionName.find(); while(yourVariableName2.hasNext()) { yourVariableName1= yourVariableName2.next(); printjson(yourVariableName1); };Let us create a collection with documents. Following is the query> db.loopThroughCollectionDemo.insertOne({"StudentName":"John", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ca81f2d6669774125247f") } > db.loopThroughCollectionDemo.insertOne({"StudentName":"Larry", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ca8272d66697741252480") } > db.loopThroughCollectionDemo.insertOne({"StudentName":"Chris", "StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ca8462d66697741252481") } > db.loopThroughCollectionDemo.insertOne({"StudentName":"Robert", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ca8632d66697741252482") }Following is the query to display all documents from a collection with the help of find() method> db.loopThroughCollectionDemo.find().pretty();This will ... Read More
644 Views
To remove all documents from a collection except a single document in MongoDB, use remove() based on some condition. Let us create a collection with documents. Following is the query>db.removeAllDocumentsExceptOneDemo.insertOne({"StudentName":"Larry", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c9de42d66697741252478") } >db.removeAllDocumentsExceptOneDemo.insertOne({"StudentName":"Mike", "StudentAge":21, "StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c9dea2d66697741252479") } >db.removeAllDocumentsExceptOneDemo.insertOne({"StudentName":"Chris", "StudentAge":24, "StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c9def2d6669774125247a") }Following is the query to display all documents from a collection with the help of find() method> db.removeAllDocumentsExceptOneDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9c9de42d66697741252478"), "StudentName" : "Larry", "StudentAge" : ... Read More
212 Views
You can use $where operator to compare field values in MongoDB. Let us first create a collection with documents> db.comparingFieldDemo.insertOne({"Value1":30, "Value2":40}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c99ed2d6669774125246e") } > db.comparingFieldDemo.insertOne({"Value1":60, "Value2":70}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c99f62d6669774125246f") } > db.comparingFieldDemo.insertOne({"Value1":160, "Value2":190}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c99ff2d66697741252470") } > db.comparingFieldDemo.insertOne({"Value1":200, "Value2":160}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c9a0b2d66697741252471") }Following is the query to display all documents from a collection with the help of find() method> db.comparingFieldDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9c99ed2d6669774125246e"), "Value1" : 30, ... Read More
369 Views
To find exact array match with values in different order, you can use $all operator. Let us create a collection with documents. Following is the query>db.exactMatchArrayDemo.insertOne({"StudentName":"David", "StudentAge":22, "StudentGameScores":[45, 78, 98]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c94702d6669774125246c") } >db.exactMatchArrayDemo.insertOne({"StudentName":"Chris", "StudentAge":23, "StudentGameScores":[45, 78]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c94a42d6669774125246d") }Following is the query to display all documents from a collection with the help of find() method> db.exactMatchArrayDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9c94702d6669774125246c"), "StudentName" : "David", "StudentAge" : 22, "StudentGameScores" : [ 45, 78, ... Read More
145 Views
Yes, you can do that. First, you need to create an index and then use explain(). Let us first create a MongoDB index. Following is the query:> db.indexOrQueryDemo.ensureIndex({"First":1});This will produce the following output{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 }The query to create second index is as follows> db.indexOrQueryDemo.ensureIndex({"Second":1});This will produce the following output{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 3, "numIndexesAfter" : 4, "ok" : 1 }Following is the query for $or operator with indexes. We have used explain() as well here> db.indexOrQueryDemo.find({$or:[{First:1}, {Second:2}]}).explain();This will produce ... Read More
1K+ Views
Following is the syntax to retrieve the documents whose values end with a particular character in MongoDBdb.yourCollectionName.find({yourFieldName: {$regex: "yourEndingCharacter$"}}).pretty();Let us first create a collection with documents>db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Adam", "StudentAge":25, "StudentCountryName":"LAOS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45b32d66697741252456") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Sam", "StudentAge":24, "StudentCountryName":"ANGOLA"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45c02d66697741252457") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Robert", "StudentAge":21, "StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45cb2d66697741252458") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Chris", "StudentAge":20, "StudentCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45d92d66697741252459") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Larry", "StudentAge":23, "StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45eb2d6669774125245a") }Following is the query to display all documents from a collection ... Read More