Found 1659 Articles for Big Data Analytics

Return query based on date in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

288 Views

To return query based on the date in MongoDB, let us take an example.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.returnQueryFromDate.insertOne({"PassengerName":"John", "PassengerAge":23, "PassengerArrivalTime":new ISODate("2018-03-10 14:45:56")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a57be9064dcd4a68b70e4") } > db.returnQueryFromDate.insertOne({"PassengerName":"Larry", "PassengerAge":21, "PassengerArrivalTime":new ISODate("2018-05-19 11:10:23")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e5") } > db.returnQueryFromDate.insertOne({"PassengerName":"Mike", "PassengerAge":24, "PassengerArrivalTime":new ISODate("2018-08-25 16:40:12")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e6") } >db.returnQueryFromDate.insertOne({"PassengerName":"Carol", "PassengerAge":26, "PassengerArrivalTime":new ISODate("2019-01-29 09:45:10")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e7") }Display all documents from a ... Read More

How to delete everything in a MongoDB database?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

425 Views

You can delete everything in a MongoDB database using dropDatabase() function. The syntax is as follows −use yourDatabaseName; db.dropDatabase();The above syntax will delete everything in a MongoDB database.To delete everything in a MongoDB database, let us first display all the databases from MongoDB. The query is as follows −> show dbsThe following is the output −use yourDatabaseName; admin 0.000GB config 0.000GB flighInformation 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB test 0.003GBNow we will delete everything from the database ‘flightInformation’.First, you need to switch the database to ‘flightInformation’. The query is as follows −> use flighInformation; switched to db flighInformationNow here ... Read More

Prettyprint in MongoDB shell as default?

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

150 Views

You can call pretty() function on cursor object to prettyprint in MongoDB shell. The syntax is as follows −db.yourCollectionName.find().pretty();To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −>db.prettyDemo.insertOne({"ClientName":"Larry", "ClientAge":27, "ClientFavoriteCountry":["US", "UK"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a440de01f572ca0ccf5f2") } >db.prettyDemo.insertOne({"ClientName":"Mike", "ClientAge":57, "ClientFavoriteCountry":["AUS", "UK"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a4420e01f572ca0ccf5f3") }Display all documents from a collection with the help of find() method. The query is as follows −> db.prettyDemo.find();The following is the output −{ "_id" : ObjectId("5c8a440de01f572ca0ccf5f2"), "ClientName" : "Larry", "ClientAge" ... Read More

How to list all collections in the Mongo shell?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

7K+ Views

To list all collections in Mongo shell, you can use the function getCollectionNames().The syntax is as follows −db.getCollectionNames();You can use another command which is collections. The syntax is as follows −show collections;To list all collections in Mongo, use the above two functions. The query is as follows −> db.getCollectionNames();The following is the output −[    "ConvertStringToDateDemo",    "IdUpdateDemo",    "ProductsInformation",    "addFieldDemo",    "addNewFieldToEveryDocument",    "arrayInnerElementsDemo",    "arrayLengthGreaterThanOne",    "arrayOfArraysDemo",    "caseInsensitiveDemo",    "changeDataType",    "changeType",    "charactersAllowedDemo",    "charactersDemo",    "checkFieldContainsStringDemo",    "checkSequenceDemo",    "combinationOfArrayDemo",    "conditionalSumDemo",    "convertStringToNumberDemo",    "copyThisCollectionToSampleDatabaseDemo",    "countDemo",    "createSequenceDemo",    "distinctCountValuesDemo",    "distinctRecordDemo", ... Read More

How do I drop a MongoDB database from the command line?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

240 Views

To drop a MongoDB database from the command line, use the following syntax:mongo yourDatabaseName --eval "db.dropDatabase()"To understand the above syntax, let us display all the database from MongoDB. The query is as follows −> show dbs;The following is the output −StudentTracker 0.000GB admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB test 0.003GBDrop the database with the name ‘StudentTracker’. The query is as follows to drop a database from command line −C:\Program Files\MongoDB\Server\4.0\bin>mongo StudentTracker --eval "db.dropDatabase()"The following is the output −MongoDB shell version v4.0.5 connecting to: mongodb://127.0.0.1:27017/StudentTracker?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("afc34e93-b4c0-46f0-85bd-b80ed17b8c11") } MongoDB server version: 4.0.5 { "dropped" ... Read More

How to count number of distinct values per field/ key in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

397 Views

You can use the distinct command for this. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.distinctCountValuesDemo.insertOne({"StudentFirstName":"John", "StudentFavouriteSubject":["C", "C++", "Java", "MySQL", "C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a39f193b406bd3df60e07") } > db.distinctCountValuesDemo.insertOne({"StudentFirstName":"Larry", "StudentFavouriteSubject":["MongoDB", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a3a1193b406bd3df60e08") }Display all documents from a collection with the help of find() method. The query is as follows −> db.distinctCountValuesDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c8a39f193b406bd3df60e07"),    "StudentFirstName" : "John",    "StudentFavouriteSubject" : [ ... Read More

Find duplicate records in MongoDB?

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

2K+ Views

You can use the aggregate framework to find duplicate records in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330293b406bd3df60e01") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330493b406bd3df60e02") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330c93b406bd3df60e03") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a331093b406bd3df60e04") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a331593b406bd3df60e05") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Mike"}); {   ... Read More

How can I use 'Not Like' operator in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

852 Views

For this, use the $not operator in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.notLikeOperatorDemo.insertOne({"StudentName":"John Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a29c393b406bd3df60dfc") } > db.notLikeOperatorDemo.insertOne({"StudentName":"John Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a29cc93b406bd3df60dfd") } > db.notLikeOperatorDemo.insertOne({"StudentName":"John Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a29df93b406bd3df60dfe") } > db.notLikeOperatorDemo.insertOne({"StudentName":"Carol Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a2a1693b406bd3df60dff") } > db.notLikeOperatorDemo.insertOne({"StudentName":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a2a2693b406bd3df60e00") }Display all documents from ... Read More

Which characters are NOT allowed in MongoDB field names?

Smita Kapse
Updated on 29-Jun-2020 16:00:38

635 Views

Do not use $ symbol or period (.) because these characters are not allowed for MongoDB field names. The field shouldn’t start with $.Here is an example of the allowed characters −> db.charactersAllowedDemo.insertOne({"Employee Name" : "John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7fefbc8d10a061296a3c6d") }Display all documents from a collection with the help of find() method. The query is as follows −> db.charactersAllowedDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c7fefbc8d10a061296a3c6d"),    "Employee Name" : "John" }

Difference between count() and find().count() in MongoDB?

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

299 Views

There is no difference between count() and find().count(). Let us see how both of them works. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.countDemo.insertOne({"UserId":1, "UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d278d10a061296a3c5d") } > db.countDemo.insertOne({"UserId":2, "UserName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d308d10a061296a3c5e") } > db.countDemo.insertOne({"UserId":3, "UserName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d3a8d10a061296a3c5f") } > db.countDemo.insertOne({"UserId":4, "UserName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d428d10a061296a3c60") }Display all documents from a collection with the help ... Read More

Advertisements