Found 1661 Articles for Big Data Analytics

Removing all collections whose name matches a string in MongoDB

George John
Updated on 30-Jul-2019 22:30:25

532 Views

To remove all collections whose name matches a string, you can follow some steps. Use for loop to iterate over all collections and find that particular collection name with certain string. After that, use the drop method to remove all collections.Let’s say we are using the database “sample”. The collections are as follows in sample database> show collections;This will produce the following outputarraySizeErrorDemo basicInformationDemo copyThisCollectionToSampleDatabaseDemo deleteAllRecordsDemo deleteDocuments deleteDocumentsDemo deleteMultipleIdsDemo deleteSomeInformation documentWithAParticularFieldValueDemo employee findListOfIdsDemo findMimimumElementInArrayDemo findSubstring getAllRecordsFromSourceCollectionDemo getElementWithMaxIdDemo insertDocumentWithDateDemo internalArraySizeDemo largestDocumentDemo makingStudentInformationClone oppositeAddToSetDemo prettyDemo returnOnlyUniqueValuesDemo selectWhereInDemo sourceCollection studentInformation sumOfValueDemo sumTwoFieldsDemo truncateDemo updateInformation userInformationNow remove all the collection names that match a ... Read More

How to delete multiple ids in MongoDB?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

1K+ Views

To delete multiple ids in MongoDB, you can use $in operator. Following is the syntaxdb.yourCollectionName.remove( { _id : { $in: [yourObjectId1, yourObjectId2, yourObjectId3)] } } );Let us create a collection with documents> db.deleteMultipleIdsDemo.insertOne({"ClientName":"Chris", "ClientAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd7d6a629b87623db1b19") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"Robert", "ClientAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd7dea629b87623db1b1a") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"Sam", "ClientAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd7e9a629b87623db1b1b") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"John", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd7f7a629b87623db1b1c") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"Carol", "ClientAge":36}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd803a629b87623db1b1d") }Following is the query to ... Read More

How to insert a document with date in MongoDB?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

3K+ 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

How to connect to my MongoDB table by command line?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

207 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

Handling optional/empty data in MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

404 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

Change collection name in MongoDB?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

215 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

How to loop through collections with a cursor in MongoDB?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

2K+ 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

How to remove all documents from a collection except a single document in MongoDB?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

788 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

How to compare field values in MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

295 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

How to find exact Array Match with values in different order using MongoDB?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

445 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

Advertisements