Found 1349 Articles for MongoDB

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

Querying an array of arrays in MongoDB?

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

758 Views

Use $in operator to query an array of arrays 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.arrayOfArraysDemo.insertOne({"EmployeeName":"Larry", "EmployeeSkills":[["Java", "MongoDB", "MySQL", "SQL Server"]]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f7a8d8d10a061296a3c5b") } > db.arrayOfArraysDemo.insertOne({"EmployeeName":"Mike", "EmployeeSkills":[["C", "C++"]]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f7aa68d10a061296a3c5c") }Display all documents from a collection with the help of find() method. The query is as follows −> db.arrayOfArraysDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c7f7a8d8d10a061296a3c5b"),    "EmployeeName" : "Larry",    "EmployeeSkills" : ... Read More

Matching an array field that contains any combination of the provided array in MongoDB?

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

190 Views

Use $nin operator along with $elemMatch and $not 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.combinationOfArrayDemo.insertOne({"StudentName":"Larry", "StudentAge":21, "StudentFavouriteTechnicalSubject":["C", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f77cc8d10a061296a3c58") } > db.combinationOfArrayDemo.insertOne({"StudentName":"Mike", "StudentAge":23, "StudentFavouriteTechnicalSubject":["C++", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f77dc8d10a061296a3c59") } > db.combinationOfArrayDemo.insertOne({"StudentName":"David", "StudentAge":22, "StudentFavouriteTechnicalSubject":["Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f77f48d10a061296a3c5a") }Display all documents from a collection with the help of find() method. The query is as follows −> db.combinationOfArrayDemo.find().pretty();The following is the output ... Read More

Advertisements