Found 1661 Articles for Big Data Analytics

How to get items with a specific value from documents using MongoDB shell?

AmitDiwan
Updated on 14-May-2020 08:28:12

474 Views

To get items with a specific value, simply use find(). Let us create a collection with documents −> db.demo563.insertOne({"Name":"Chris", "Age":21, "isMarried":true}){    "acknowledged" : true, "insertedId" : ObjectId("5e8f546c54b4472ed3e8e878") } > db.demo563.insertOne({"Name":"Bob", "Age":23, "isMarried":false}){    "acknowledged" : true, "insertedId" : ObjectId("5e8f547854b4472ed3e8e879") } > db.demo563.insertOne({"Name":"Carol", "Age":23, "isMarried":true}){    "acknowledged" : true, "insertedId" : ObjectId("5e8f548b54b4472ed3e8e87a") } > db.demo563.insertOne({"Name":"Mike", "Age":21, "isMarried":true}){    "acknowledged" : true, "insertedId" : ObjectId("5e8f549454b4472ed3e8e87b") }Display all documents from a collection with the help of find() method −> db.demo563.find();This will produce the following output −{ "_id" : ObjectId("5e8f546c54b4472ed3e8e878"), "Name" : "Chris", "Age" : 21, "isMarried" : true } { "_id" : ... Read More

How to search for a record (field) and then delete it in MongoDB?

AmitDiwan
Updated on 14-May-2020 08:26:23

471 Views

To search for a field, use $exists and to delete it, use $unset. The $unset operator in MongoDB deletes a particular field.Let us create a collection with documents −> db.demo562.insertOne({"Name":"Chris", "Age":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8f4ae854b4472ed3e8e872") } > db.demo562.insertOne({"Age":20});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8f4ae954b4472ed3e8e873") } > db.demo562.insertOne({"Name":"David", "Age":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8f4aea54b4472ed3e8e874") }Display all documents from a collection with the help of find() method −> db.demo562.find();This will produce the following output −{ "_id" : ObjectId("5e8f4ae854b4472ed3e8e872"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e8f4ae954b4472ed3e8e873"), "Age" : 20 } ... Read More

Randomizing unique data with MongoDB and placing values for emailid with wordJohn in the beginning

AmitDiwan
Updated on 14-May-2020 08:21:48

96 Views

To randomize unique data, use Math.random() in MongoDB. Let us create a collection with documents −> db.demo561.insertOne({EmailId:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f490454b4472ed3e8e86c") } > db.demo561.insertOne({EmailId:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f490654b4472ed3e8e86d") } > db.demo561.insertOne({EmailId:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f490a54b4472ed3e8e86e") }Display all documents from a collection with the help of find() method −> db.demo561.find();This will produce the following output −{ "_id" : ObjectId("5e8f490454b4472ed3e8e86c"), "EmailId" : null } { "_id" : ObjectId("5e8f490654b4472ed3e8e86d"), "EmailId" : null } { "_id" : ObjectId("5e8f490a54b4472ed3e8e86e"), "EmailId" : null }Following is the query for randomizing unique data with MongoDB −> db.demo561.find().forEach(function(doc){ ...   ... Read More

Fetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?

AmitDiwan
Updated on 14-May-2020 08:17:34

655 Views

To match, use $match in MongoDB and to get data between two dates, use $gte and $lte. Let us create a collection with documents −> db.demo560.insertOne({"value1":40, "value2":40, shippingDate:new ISODate("2020-02-26")});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e867") } > db.demo560.insertOne({"value1":20, "value2":60, shippingDate:new ISODate("2020-02-26")});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e868") } > db.demo560.insertOne({"value1":40, "value2":70, shippingDate:new ISODate("2020-03-31")});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e869") } > db.demo560.insertOne({"value1":40, "value2":130, shippingDate:new ISODate("2020-03-31")});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e86a") }Display all documents from a collection with the help of find() method −> db.demo560.find();This will produce the following output −{ "_id" : ObjectId("5e8f3d5254b4472ed3e8e867"), "value1" : ... Read More

GROUP BY array of document to get the count of repeated Age values

AmitDiwan
Updated on 14-May-2020 08:14:51

154 Views

To GROUP BY array of the document, use $group. Let us create a collection with documents −>db.demo559.insertOne({details:[{Name:"Chris", Age:21}, {Name:"Bob", Age:22}, {Name:"Carol", Age:21}, {Name:"Sam", Age:21}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e8f38d954b4472ed3e8e866") }Display all documents from a collection with the help of find() method −> db.demo559.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e8f38d954b4472ed3e8e866"),    "details" : [       {          "Name" : "Chris",          "Age" : 21       },       {          "Name" : "Bob",          "Age" : ... Read More

MongoDB aggregate to get the count of field values of corresponding duplicate names?

AmitDiwan
Updated on 14-May-2020 08:11:54

1K+ Views

Let us see an example and create a collection with documents −> db.demo558.insertOne( ... { ...    _id : 100, ...    CountryCode:101, ...    details: [ ...       { ...          Name:"Chris", ...          Subject:"MySQL" ...       }, ...       { ...          Name:"Chris", ...          Subject:"MongoDB" ...       }, ...       { ...          Name:"Chris", ...          Subject:"Java" ...       }, ...       { ...   ... Read More

How to run MongoDB query to update only a specific field value?

AmitDiwan
Updated on 14-May-2020 08:08:49

220 Views

Let us see an example and create a collection with documents −> db.demo557.insertOne({Name:"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f28e954b4472ed3e8e864") } > db.demo557.insertOne({Name:"David"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f28ee54b4472ed3e8e865") }Display all documents from a collection with the help of find() method −> db.demo557.find();This will produce the following output −{ "_id" : ObjectId("5e8f28e954b4472ed3e8e864"), "Name" : "Chris" } { "_id" : ObjectId("5e8f28ee54b4472ed3e8e865"), "Name" : "David" }Following is the query to update only a specific field value −> db.getCollection('demo557').update({Name:"Chris"}, {$set:{Name:"Robert"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of find() method ... Read More

Fetch records from a subdocument array wherein id begins from 234 in MongoDB

AmitDiwan
Updated on 14-May-2020 08:07:30

113 Views

To fetch records from a subdocument array, use $unwind along with $push. For ids beginning from 234, use regex in MongoDB.Let us create a collection with documents −> db.demo556.insertOne( ... { ...    _id:101, ...    details:[ ...       { ...          id:"234336", ...          Name:"Chris" ...       }, ...       { ...          id:"123456", ...          Name:"Bob" ...       }, ...       { ...          id:"234987", ...          Name:"Carol" ...   ... Read More

MongoDB query (aggregation framework) to match a specific field value

AmitDiwan
Updated on 14-May-2020 08:04:09

310 Views

To match a specific field value, use a $match in MongoDB aggregation. Let us create a collection with documents −> db.demo555.insertOne({"CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21bf54b4472ed3e8e85f") } > db.demo555.insertOne({"CountryName":"UK"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21c254b4472ed3e8e860") } > db.demo555.insertOne({"CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21c354b4472ed3e8e861") } > db.demo555.insertOne({"CountryName":"AUS"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21c554b4472ed3e8e862") } > db.demo555.insertOne({"CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21c754b4472ed3e8e863") }Display all documents from a collection with the help of find() method −> db.demo555.find();This will produce the following output: −{ "_id" : ObjectId("5e8f21bf54b4472ed3e8e85f"), "CountryName" : "US" } { "_id" : ObjectId("5e8f21c254b4472ed3e8e860"), "CountryName" ... Read More

Query MongoDB with “like” implementation on name and email field beginning with a specific letter?

AmitDiwan
Updated on 14-May-2020 08:02:33

913 Views

For “like” implementation in MongoDB, use / / and set that specific letter in between. For example −/J/Let us create a collection with documents −> db.demo554.insertOne({"UserName":"John", "UserMailId":"John@gmail.com"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f1cfed1d72c4545cb8679") } > db.demo554.insertOne({"UserName":"Chris", "UserMailId":"Chris@gmail.com"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f1d0cd1d72c4545cb867a") } > db.demo554.insertOne({"UserName":"Jace", "UserMailId":"Jace@gmail.com"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f1d1cd1d72c4545cb867b") }Display all documents from a collection with the help of find() method −> db.demo554.find();This will produce the following output −{ "_id" : ObjectId("5e8f1cfed1d72c4545cb8679"), "UserName" : "John", "UserMailId" : "John@gmail.com" } { "_id" : ObjectId("5e8f1d0cd1d72c4545cb867a"), "UserName" : "Chris", "UserMailId" : "Chris@gmail.com" } { "_id" : ... Read More

Advertisements