Found 1659 Articles for Big Data Analytics

Get a single element from the array of results by index in MongoDB

AmitDiwan
Updated on 02-Apr-2020 14:12:59

475 Views

To get a single element, use aggregation and LIMIT. The skip() is used to skip a specific number of documents.Let us first create a collection with documents −> db.demo391.insertOne( ...    { "_id" : 101, "Name" : "Chris", Values: ["101", "102"] } ... ) { "acknowledged" : true, "insertedId" : 101 } > db.demo391.insertOne( ...    { "_id" : 111, "Name" : "Chris", Values: ["101", "102"] } ... ) { "acknowledged" : true, "insertedId" : 111 } > db.demo391.insertOne( ...    { "_id" : 121, "Name" : "Chris", Values: ["101", "102"] } ... ) { "acknowledged" : true, "insertedId" : ... Read More

Update values in multiple documents with multi parameter in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 14:09:16

365 Views

You need to set multi to true. Include the option multi − true to update all documents that match the query criteria.Let us first create a collection with documents −> db.demo390.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5d1f3a22064be7ab44e7fa") } > db.demo390.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5d1f3e22064be7ab44e7fb") } > db.demo390.insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5d1f4122064be7ab44e7fc") } > db.demo390.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5d1f4422064be7ab44e7fd") }Display all documents from a collection with the help of find() method −> db.demo390.find();This will produce the following output −{ "_id" : ObjectId("5e5d1f3a22064be7ab44e7fa"), "FirstName" ... Read More

Extract all the values and display in a single line with MongoDB

AmitDiwan
Updated on 02-Apr-2020 13:59:03

419 Views

Let us first create a collection with documents −> db.demo389.insertOne( ...    { ...       "details":[ ...          { ...             "Name":[ ...                "Chris", ...                "David" ...             ] ...          }, ...          { ...             "Name":[ ...                "Bob", ...                "Mike" ...       ... Read More

MongoDB query to remove element from array as sub property

AmitDiwan
Updated on 02-Apr-2020 13:55:18

300 Views

To remove, use $pull in MongoDB. Let us first create a collection with documents −> db.demo388.insertOne( ...    { ...       _id: '101', ...       userDetails: { ...          isMarried: false, ...          userInfo: [ ...             { ...                Name:"Chris", ...                Age:21 ... ...             } ...          ] ...       } ...    } ... ); { "acknowledged" : true, ... Read More

MongoDB query to unwind two arrays

AmitDiwan
Updated on 02-Apr-2020 13:52:58

506 Views

To unwind, means to deconstruct an array field from the input documents to output a document for each element.To unwind arrays, use $unwind in MongoDB aggregation. Let us first create a collection with documents −> db.demo387.insertOne( ...    { ... ...       "Name" : "101", ...       "Details1" : [ ...          {Value:100, Value1:50, Value2:40}, ...          {Value:200}, ...          {Value:300} ...       ], ...       "Details" : [ ...          {Value:100, Value1:30, Value2:26}, ...          {Value:200}, ... Read More

Convert date parts to date in MongoDB

AmitDiwan
Updated on 02-Apr-2020 13:49:36

247 Views

Let us first create a collection with documents −> db.demo386.insert( ...    { ...       details: { Month: 02, Day: 27, Year: 2020 } ...    } ... ); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo386.find();This will produce the following output −{ "_id" : ObjectId("5e5bd9a222064be7ab44e7f7"), "details" : { "Month" : 2, "Day" : 27, "Year" : 2020 } }Following is the query to convert date parts to date −> db.demo386.aggregate( ...    {"$project":{ ...       "_id":0, ...       "DueDate":{ ...          "$dateToString":{ ...             "format":"%m-%d-%Y", ...             "date":{"$dateFromParts": {"year":"$details.Year","month":"$details.Month","day":"$details.Day"}} ...          } ...       } ...    }} ... );This will produce the following output −{ "DueDate" : "02-27-2020" }

Update multiple elements in an array in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 13:47:05

1K+ Views

To update multiple elements, use $[]. The $[] is an all positional operator indicating that the update operator should modify all elements in the specified array field.Let us first create a collection with documents −> db.demo385.insertOne({"ServerLogs": [ ...       { ...          "status":"InActive" ...       }, ...       { ...          "status":"InActive" ...       }, ...       { ...          "status":"InActive" ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ... Read More

Display only a single field from all the documents in a MongoDB collection

AmitDiwan
Updated on 02-Apr-2020 13:44:15

625 Views

Projection means only selected field must be visible. Set the field to 1, if you want to make it visible.Let us first create a collection with documents −> db.demo384.insertOne({"StudentName":"Chris Brown", "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5b67a022064be7ab44e7f2") } > db.demo384.insertOne({"StudentName":"David Miller", "StudentCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5b67ab22064be7ab44e7f3") } > db.demo384.insertOne({"StudentName":"John Doe", "StudentCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5b67b422064be7ab44e7f4") }Display all documents from a collection with the help of find() method −> db.demo384.find();This will produce the following output −{ "_id" : ObjectId("5e5b67a022064be7ab44e7f2"), "StudentName" : "Chris Brown", "StudentCountryName" : "US" } { ... Read More

MongoDB query to filter only the logs containing the “work” word in the content

AmitDiwan
Updated on 02-Apr-2020 13:41:52

134 Views

To filter the logs containing the word “work” , use aggregate() along with $filter. Let us first create a collection with documents −> db.demo383.insertOne( ...    { ...       "ServerName":"Jboss", ...       "ServerLogs": [ ...          { ...             "status":"Working" ...          }, ...          { ...             "status":"Stop" ...          }, ...          { ...             "status":"Worked" ...          } ...   ... Read More

MongoDB aggregate $slice to get the length of the array

AmitDiwan
Updated on 02-Apr-2020 13:37:46

356 Views

For this, use $project and in that, $size to get the length. Let us first create a collection with documents −> db.demo382.insertOne( ...    { ... ...       "Name" : "David", ...       "details" : [ ...          { ...             "SubjectName":"MySQL" ...          }, ...          { ...             "SubjectName":"MongoDB" ...          }, ...          { ...             "SubjectName":"Java" ...          } ... Read More

Advertisements