Found 1659 Articles for Big Data Analytics

Shutdown MongoDB with auth enabled?

AmitDiwan
Updated on 01-Apr-2020 14:40:23

89 Views

To shutdown MongoDB, you need to use shutdownServer() as in the below syntax −db.shutdownServer();First you need to switch to admin as shown below −use admin;Here, we switched to admin −> use admin; switched to db adminFollowing is the query to shutdown server −> db.shutdownServer();This will produce the following output −server should be down... 2020-01-07T22:40:31.295+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed 2020-01-07T22:40:32.326+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed

Get the number of open connections in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 05:37:14

350 Views

To get the number of open connections, use serverStatus() in MongoDB. Following is the query −> db.serverStatus();This will produce the following output −{    "host" : "DESKTOP-QN2RB3H",    "version" : "4.0.5",    "process" : "mongod",    "pid" : NumberLong(10540),    "uptime" : 74156,    "uptimeMillis" : NumberLong(74156688),    "uptimeEstimate" : NumberLong(74156),    "localTime" : ISODate("2020-01-07T15:09:18.366Z"),    "asserts" : {       "regular" : 0,       "warning" : 0,       "msg" : 0,       "user" : 1,       "rollovers" : 0    },    "connections" : {       "current" : ... Read More

How to delete particular data in a document in MongoDB?

AmitDiwan
Updated on 01-Apr-2020 14:31:47

155 Views

You can use $unset. Let us first create a collection with documents −> db.demo20.insertOne( ...    { ... ...       "ListOfEmployee" : [ ...          { ...             "EmployeeName1" : "John" ...          }, ...          { ...             "EmployeeName2" : "Carol" ...          } ...       ], ...       "EmployeeName2" : [] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e138c3555d0fc6657d21f12") }Following is the query to ... Read More

Using a regex with text search in MongoDB

AmitDiwan
Updated on 01-Apr-2020 14:30:21

464 Views

To filter records using Regular Expression in MongoDB, use $regex. Let us first create a collection with documents −> db.demo19.insertOne({"Values":"4321GH"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1389b955d0fc6657d21f0f") } > db.demo19.insertOne({"Values":"12321_Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1389c755d0fc6657d21f10") } > db.demo19.insertOne({"Values":"8765Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1389d355d0fc6657d21f11") }Following is the query to display all documents from a collection with the help of find() method −> db.demo19.find();This will produce the following output −{ "_id" : ObjectId("5e1389b955d0fc6657d21f0f"), "Values" : "4321GH" } { "_id" : ObjectId("5e1389c755d0fc6657d21f10"), "Values" : "12321_Carol" } { "_id" : ObjectId("5e1389d355d0fc6657d21f11"), "Values" : "8765Mike" ... Read More

Convert a field to an array using update operation in MongoDB

AmitDiwan
Updated on 01-Apr-2020 14:28:48

164 Views

To convert a field to an array, use UPDATE operation inside forEach(). Let us first create a collection with documents −> db.demo18.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1387fc55d0fc6657d21f0e") }Following is the query to display all documents from a collection with the help of find() method −> db.demo18.find();This will produce the following output −{ "_id" : ObjectId("5e1387fc55d0fc6657d21f0e"), "StudentName" : "John" }Here is the query to convert a field to an array using update operation −> db.demo18.find().forEach(function(myDocument) { ...    db.demo18.update( ...       { _id: myDocument._id }, ...       { "$set": { "StudentName": [myDocument.StudentName] } ... Read More

Filter documents in MongoDB if all keys exist as fields?

AmitDiwan
Updated on 01-Apr-2020 14:26:33

260 Views

For this, use $all, that would find documents containing all of the elements in an array like "keys". Let us first create a collection with documents −> db.demo17.insertOne({"ListOfSubject":["MySQL", "MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e13847255d0fc6657d21f0a") } > db.demo17.insertOne({"ListOfSubject":["C", "Python", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e13847e55d0fc6657d21f0b") } > db.demo17.insertOne({"ListOfSubject":["C++", "MongoDB", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e13849255d0fc6657d21f0c") }Following is the query to display all documents from a collection with the help of find() method −> db.demo17.find();This will produce the following output −{ "_id" : ObjectId("5e13847255d0fc6657d21f0a"), "ListOfSubject" : [ "MySQL", "MongoDB", ... Read More

Perform group and distinct together in a single MongoDB query

AmitDiwan
Updated on 01-Apr-2020 14:25:02

373 Views

For this, simply use MongoDB $group. Let us first create a collection with documents −> db.demo16.insertOne({ ...    "StudentName" : "Chris", ...    "StudentSection" : "A", ...    "StudentAge" : 23, ...    "StudentMarks" : 47 ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e13827455d0fc6657d21f07") } > db.demo16.insertOne({ ...    "StudentName" : "Bob", ...    "StudentSection" : "B", ...    "StudentAge" : 21, ...    "StudentMarks" : 85 ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e13827555d0fc6657d21f08") } > db.demo16.insertOne( { ...    "StudentName" : "Carol", ...    "StudentSection" : "A", ... ... Read More

MongoDB: How to query a collection named “version”?

AmitDiwan
Updated on 01-Apr-2020 14:21:57

100 Views

For this, use the concept of createCollection() and getCollection(). Following is the query to create a collection with name ‘version’ −> db.createCollection('version'); { "ok" : 1 }Let us first create a collection with documents −> db.getCollection('version').insertOne({"VersionName":"1.0"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e100b47d7df943a7cec4fae") } > db.getCollection('version').insertOne({"VersionName":"1.1"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e100b49d7df943a7cec4faf") } > db.getCollection('version').insertOne({"VersionName":"1.2"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e100b4cd7df943a7cec4fb0") }Following is the query to display all documents from a collection with the help of find() method −> db.getCollection('version').find();This will produce the following output −{ "_id" : ObjectId("5e100b47d7df943a7cec4fae"), "VersionName" : "1.0" ... Read More

Find MongoDB documents where elements of an array have a specific value?

AmitDiwan
Updated on 01-Apr-2020 14:20:07

100 Views

To match documents in MongoDB, use $elemMatch. Let us first create a collection with documents −> db.demo15.insertOne({"Details":[{"Score":56}, {"Score":78}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f7806d7df943a7cec4fab") } > db.demo15.insertOne({"Details":[{"Score":86}, {"Score":86}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f7817d7df943a7cec4fac") } > db.demo15.insertOne({"Details":[{"Score":97}, {"Score":85}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f7823d7df943a7cec4fad") }Following is the query to display all documents from a collection with the help of find() method −> db.demo15.find();This will produce the following output −{ "_id" : ObjectId("5e0f7806d7df943a7cec4fab"), "Details" : [ { "Score" : 56 }, { "Score" : 78 } ] } { "_id" : ObjectId("5e0f7817d7df943a7cec4fac"), ... Read More

MongoDB query for array concatenation?

AmitDiwan
Updated on 01-Apr-2020 14:18:15

118 Views

To concatenate, use $concatArrays in MongoDB. Let us first create a collection with documents −>db.demo14.insertOne({"ListOfStudent":["Carol", "Mike", "Sam"], "ListOfTeacher":["Robert", "David"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f754bd7df943a7cec4faa") }Following is the query to display all documents from a collection with the help of find() method −> db.demo14.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e0f754bd7df943a7cec4faa"),    "ListOfStudent" : [       "Carol",       "Mike",       "Sam"    ],    "ListOfTeacher" : [       "Robert",       "David"    ] }Following is the query to concatenate array −> db.demo14.aggregate([ ... { ... Read More

Advertisements