AmitDiwan has Published 10744 Articles

MongoDB difference between show dbs and show databases?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 13:13:04

307 Views

There is no difference between show dbs and show databases. Both commands internally call listDatabases command.The show dbs command is as follows −> show dbsThis will produce the following output −admin             0.002GB app               ... Read More

Find when the keys are unknown in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 13:08:30

695 Views

To find when the keys are unknown, use $addField and $objectToArray. Let us first create a collection with documents −> db.demo375.insertOne( ...    { ...       "details":{ ...          "Name":"John", ...          "Age":23 ...       } ...    } ... ... Read More

Find values group by another field in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 13:04:56

471 Views

To group by another field, use $group along with $project. Let us first create a collection with documents −> db.demo374.insertOne( ...    { ... ...       "Name" : "Chris", ...       "HobbyDetails" : [ ...          "Reading Book", ...         ... Read More

How to index and sort with pagination using custom field in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 13:02:12

404 Views

Let us first create a collection with documents −> db.demo373.createIndex({"Name":1, "CountryName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo373.insertOne({"Name":"Chris", "Age":22, "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e59ffde2ae06a1609a00aff") } > db.demo373.insertOne({"Name":"David", "Age":21, "CountryName":"AUS"}); {    "acknowledged" ... Read More

How to use $ifNull with MongoDB aggregation?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 13:00:31

2K+ Views

The $ifNull evaluates an expression and returns the value of the expression if the expression evaluates to a non-null value.Let us first create a collection with documents −> db.demo372.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e591aea2ae06a1609a00af6") } > db.demo372.insertOne({"FirstName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e591aef2ae06a1609a00af7") ... Read More

Changing the primary key on a MongoDB collection?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:59:29

1K+ Views

To change the primary key, you need to first delete it. Use forEach() along with delete to remove and then get a new primary key. Let us create a collection with documents −> db.demo41.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e25ce4acfb11e5c34d898e3") }Display all documents from a collection with ... Read More

Search by specific field in MongoDB

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:58:08

317 Views

Let us first create a collection with documents −> db.demo371.insertOne({"Name":"David", "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57f6982ae06a1609a00af2") } > db.demo371.insertOne({"Name":"John", "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57f69e2ae06a1609a00af3") } > db.demo371.insertOne({"Name":"Bob", "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57f6a42ae06a1609a00af4") } > db.demo371.insertOne({"Name":"Mike", "CountryName":"US"}); ... Read More

How to create a new object and get its saved object in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:57:56

2K+ Views

For this, use save() in MongoDB. Following is the syntax −var anyVaribaleName=yourValue db.anyCollectionName.save(yourVariableName); yourVariableName;Let us first create an object for our example −> var studentDetails={"StudentName":"Chris", "ListOfMarks":[56, 78, 89], "ListOfSubject":["MySQL", "Java", "MongoDB"]};Let us save the above created object “studentDetails” −> db.demo40.save(studentDetails); WriteResult({ "nInserted" : 1 })Let us display the value −> ... Read More

MongoDB query for documents matching array, irrespective of elements order

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:56:10

238 Views

For this, use $all in MongoDB. The $all operator in MongoDB selects the documents where the value of a field is an array that contains all the specified elements.Let us first create a collection with documents −> db.demo370.insertOne( ...    { ...       "Name" : "Chris", ...   ... Read More

Display collections in a particular MongoDB database?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:55:58

164 Views

At first, switch to a particular database in MongoDB with the USE command as in the below syntax −use yourDatabaseName; db.getCollectionNames();Let us implement the above syntax to display collections of database WEB −> use web; switched to db web > db.getCollectionNames();This will produce the following output −[ ... Read More

Advertisements