Found 6705 Articles for Database

MongoDB query to unwind two arrays

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

686 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

322 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

2K+ 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

744 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

171 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

446 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

How to search an array for values present in another array and output the indexes of values found into a new array in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 13:35:22

629 Views

For this, use $indexOfArray. Let us first create a collection with documents −> db.demo381.insertOne({"Values":[10, 40, 60, 30, 60]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5b59f72ae06a1609a00b15") } > db.demo381.insertOne({"Values":[100, 500, 700, 500, 800]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5b59f72ae06a1609a00b16") } > db.demo381.insertOne({"Values":[20, 40, 30, 10, 60]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5b59f72ae06a1609a00b17") }Display all documents from a collection with the help of find() method −> db.demo381.find();This will produce the following output −{ "_id" : ObjectId("5e5b59f72ae06a1609a00b15"), "Values" : [ 10, 40, 60, 30, 60 ] } { "_id" : ObjectId("5e5b59f72ae06a1609a00b16"), "Values" : [ 100, ... Read More

MongoDB $addToSet to add a deep nested array of object?

AmitDiwan
Updated on 02-Apr-2020 13:32:30

1K+ Views

The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us first create a collection with documents −> db.demo380.insertOne({ ... ...    "details" : [ ...       { ...          "Name" : "Chris", ...          "details1" : [ ] ...       }, ...       { ...          "Name" : "David", ...          "details1" : [ ] ...       } ...    ] ... } ... ); ... Read More

MongoDB projection on specific nested properties?

AmitDiwan
Updated on 02-Apr-2020 13:29:06

1K+ Views

For projection on specific nested properties, use aggregate() in MongoDB. Let us first create a collection with documents −> db.demo379.insertOne( ...    { ...       "details1" : { ...          "details2" : { ...             "details3" : { ...                "10" : "John", ...                "50" : "Chris", ...                "40" : "David", ...                "30":"Mike" ...             } ...   ... Read More

Manipulating subdocuments in MongoDB

AmitDiwan
Updated on 02-Apr-2020 13:25:17

180 Views

To manipulate subdocuments, use dot(.) notation in MongoDB. Let us first create a collection with documents −> db.demo378.insertOne( ...    { ...       Name: 'Chris', ...       details:[ ...          {id:101, Score:56}, ...          {id:102, Score:78} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5a758a2ae06a1609a00b0f") }Display all documents from a collection with the help of find() method −> db.demo378.find();This will produce the following output −{    "_id" : ObjectId("5e5a758a2ae06a1609a00b0f"), "Name" : "Chris", "details" : [       { "id" ... Read More

Advertisements