Found 1349 Articles for MongoDB

Update only specific fields in MongoDB?

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

670 Views

To update only specific field, you can use $set operator. Let us first create a collection with documents>db.updateOnlySpecificFieldDemo.insertOne({"EmployeeName":"John", "EmployeeCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ea849d628fa4220163b72") } >db.updateOnlySpecificFieldDemo.insertOne({"EmployeeName":"Larry", "EmployeeCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ea853d628fa4220163b73") } >db.updateOnlySpecificFieldDemo.insertOne({"EmployeeName":"David", "EmployeeCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ea85bd628fa4220163b74") }Following is the query to display all documents from a collection with the help of find() method> db.updateOnlySpecificFieldDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9ea849d628fa4220163b72"),    "EmployeeName" : "John",    "EmployeeCountryName" : "UK" } {    "_id" : ObjectId("5c9ea853d628fa4220163b73"),    "EmployeeName" : "Larry",    "EmployeeCountryName" : ... Read More

How to return only value of a field in MongoDB?

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

796 Views

In order to return only value of a field in MongoDB, you need to write a query and use forEach loop. Let us first create a collection with documents> db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ea537d628fa4220163b6e") } > db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ea53bd628fa4220163b6f") } > db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ea541d628fa4220163b70") } > db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Ramit"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ea549d628fa4220163b71") }Following is the query to display all documents from a collection with the help of find() method> db.returnOnlyValueOfFieldDemo.find().pretty();This will produce the following output{ "_id" : ... Read More

Write a MongoDB query to get nested value?

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

133 Views

You can use dot notation to get nested value. Let us first create a collection with documents> db.nestedQueryDemo.insertOne( ...    { ... ...       "EmployeeName" : "John", ...       "EmployeeDetails" : ...       { ... ...          "_id":"EMP-101", ...          "EmployeeAge":23, ...          "EmployeeCompanyName":"IBM" ... ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ea31dd628fa4220163b69") } > db.nestedQueryDemo.insertOne( ...    { ... ...       "EmployeeName" : "Carol", ...       "EmployeeDetails" : ... ... Read More

Get component of Date / ISODate in MongoDB?

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

161 Views

To get component of Date/ISODate in MongoDB, let us create a document with date in the collection. Now let us get the component of Date/ISODate in MongoDB> db.componentOfDateDemo.insert({"ShippingDate":new Date()}); WriteResult({ "nInserted" : 1 })Following is the query to display all documents from a collection with the help of find() method> db.componentOfDateDemo.find().pretty()This will produce the following output{    "_id" : ObjectId("5c9e9d57d628fa4220163b68"),    "ShippingDate" : ISODate("2019-03-29T22:33:59.776Z") }Following is the query to get result using findOne()> var result=db.componentOfDateDemo.findOne();Now you can display documents from the collection. Following is the query> resultThis will produce the following output{    "_id" : ObjectId("5c9e9d57d628fa4220163b68"),    "ShippingDate" : ISODate("2019-03-29T22:33:59.776Z") ... Read More

Find the document by field name with a specific value in MongoDB?

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

509 Views

To find the document by field name with a specific value, you can use $exists operator. Let us create a collection with documents> db.findByFieldName.insertOne( { "Client":{ "ClientDetails":{ "ClientName":"Larry", "ClientAge":29 }, "ClientProjectDetails":{ "ProjectName":"Online Book Store", "TeamSize":10, "TechnologyUsed":"Spring Boot" } } } ); { "acknowledged" : true, "insertedId" : ObjectId("5c9e93b2d628fa4220163b64") } > db.findByFieldName.insertOne({ ... "   Client":{ ... "      ClientDetails":{ ... "         ClientName":"Chris", ... "         ClientAge":27 ...        }, ...       "ClientEducationDetails":{ ... "         isEducated":true, ...          "CollegeName":"M.I.T." ... ...   ... Read More

Check if MongoDB database exists?

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

2K+ Views

There are two possibilities to check if MongoDB database exists.Case 1: The first possibility is that the MongoDB database exists i.e. it returns particular index.Case 2: The second possibility is that the MongoDB database does not exist i.e. it returns index -1.NOTE: An index starts from 0 and ends with (N-1) like an array.The syntax is as follows to check if MongoDB database exists.db.getMongo().getDBNames().indexOf("yourDatabaseName");Case 1: Let us implement the above syntax to check if MongoDB database exists. Following is the querydb.getMongo().getDBNames().indexOf("test");This will produce the following output6Look at the above sample output, we are getting 6 that means the database “test” ... Read More

Get MongoDB Databases in a JavaScript Array?

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

199 Views

To get MongoDB databases in a JavaScript array, you can use runCommand(). Following is the query to get MongoDB databases in a JavaScript array> use admin; switched to db admin > allDatabasesDetails = db.runCommand({listDatabases: 1});This will produce the following output{    "databases" : [       {          "name" : "admin",          "sizeOnDisk" : 847872,          "empty" : false       },       {          "name" : "config",          "sizeOnDisk" : 98304,          "empty" : false     ... Read More

How to find a record by _id in MongoDB?

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

601 Views

In order to find record by _id in MongoDB, you can use the following syntaxdb.yourCollectionName.find({"_id":yourObjectId});Let us create a collection with documents> db.findRecordByIdDemo.insertOne({"CustomerName":"Larry", "CustomerAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9dc2c875e2eeda1d5c3671") } > db.findRecordByIdDemo.insertOne({"CustomerName":"Bob", "CustomerAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9dc2d175e2eeda1d5c3672") } > db.findRecordByIdDemo.insertOne({"CustomerName":"Carol", "CustomerAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9dc2d775e2eeda1d5c3673") } > db.findRecordByIdDemo.insertOne({"CustomerName":"David", "CustomerAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9dc2e375e2eeda1d5c3674") }Following is the query to display all documents from a collection with the help of find() method> db.findRecordByIdDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9dc2c875e2eeda1d5c3671"),    "CustomerName" ... Read More

How to improve querying field in MongoDB?

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

82 Views

To improve querying field in MongoDB, you need to use index. Let us create a collection with documents> db.improveQueryDemo.insertOne( ... { ...    "PlayerDetails":[ ...       {"PlayerName": "John", "PlayerGameScore": 5690}, ...       {"PlayerName": "Carol", "PlayerGameScore": 2690}, ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9dbaf875e2eeda1d5c3670") }Following is the query to display all documents from a collection with the help of find() method> db.improveQueryDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9dbaf875e2eeda1d5c3670"),    "PlayerDetails" : [       {          "PlayerName" : "John",     ... Read More

MongoDB equivalent of SELECT field AS `anothername`?

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

2K+ Views

In MySQL, we give an alias name for a column. Similarly, you can give an alias name for field name in MongoDB. The MongoDB equivalent syntax is as followsdb.yourCollectionName.aggregate( [    { "$project": {       "_id": 0,       "anyAliasName": "$yourFieldName"    }} ]);Let us first create a collection with documents> db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d448827b86948e204ca91") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d449027b86948e204ca92") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d449527b86948e204ca93") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d449927b86948e204ca94") ... Read More

Advertisements