Found 1659 Articles for Big Data Analytics

How to get connected clients in MongoDB?

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

599 Views

To get connected clients in MongoDB, use currentOp() with the set value to true and you need to iterate array result set with the help of field client. Let us first implement currentOp> db.currentOp(true)Following is the output. Here the client is 127.0.0.1 since we are using localhost. The output displays all the connected clients{    "inprog" : [       {          "host" : "DESKTOP-QN2RB3H:27017",          "desc" : "conn1",          "connectionId" : 1,          "client" : "127.0.0.1:61787",          "appName" : "MongoDB Shell",     ... Read More

How do you limit an array sub-element in MongoDB?

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

660 Views

You can use $slice operator to limit an array. Let us create a collection with documents. Following is the query> db.limitAnArrayDemo.insertOne( ...    { ...       _id: 101, ...       "PlayerName": "Bob", ...       "PlayerDetails": {Age:23, isStudent:true}, ...       "PlayerGameScores": [234, 5767, 58, 67, 78, 90, 1002, 576, 68, 45, 23, 45, 678, 89, 78 ] ...    } ... ); { "acknowledged" : true, "insertedId" : 101 }Following is the query to display all documents from a collection with the help of find() method> db.limitAnArrayDemo.find().pretty();This will produce the following output{   ... Read More

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

Advertisements