Found 1659 Articles for Big Data Analytics

How to insert Date in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 13:47:42

974 Views

To insert date in MongoDB, use Date(). Let us create a collection with documents −> db.demo421.insert({"DueDate":new Date(Date.now())}); WriteResult({ "nInserted" : 1 }) > db.demo421.insert({"DueDate":new Date("2020-01-15")}); WriteResult({ "nInserted" : 1 }) > db.demo421.insert({"DueDate":new Date("2018-12-31")}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo421.find();This will produce the following output −{ "_id" : ObjectId("5e73a2ec9822da45b30346de"), "DueDate" : ISODate("2020-03-19T16:50:52.746Z") } { "_id" : ObjectId("5e73a2fb9822da45b30346df"), "DueDate" : ISODate("2020-01-15T00:00:00Z") } { "_id" : ObjectId("5e73a3079822da45b30346e0"), "DueDate" : ISODate("2018-12-31T00:00:00Z") }Read More

Combining unique items from arrays in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 13:46:05

420 Views

To combine unique items from array, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo420.insert( ...    { ... ...       "details" : [ ...          { ...             "Value1":10, ...             "Value2":20, ...             "Value3":30 ...          } ...       ] ...    } ... ) WriteResult({ "nInserted" : 1 }) > db.demo420.insert( ...    { ... ...       "Info" : [ ...          { ... Read More

Update object in array with a specific key in MongoDB

AmitDiwan
Updated on 03-Apr-2020 13:42:15

683 Views

Let us first create a collection with documents −>db.demo419.insertOne({"ProductInformation":[{"ProductName":"Product-1", "ProductPrice":500}, {"ProductName":"Product-2", "ProductPrice":600}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e724762b912067e57771ae8") }Display all documents from a collection with the help of find() method −> db.demo419.find();This will produce the following output −{ "_id" : ObjectId("5e724762b912067e57771ae8"), "ProductInformation" : [ { "ProductName" : "Product-1", "ProductPrice" : 500 }, { "ProductName" : "Product-2", "ProductPrice" : 600 } ] }Following is the query to update object in array with a specific key −> db.demo419.update({'ProductInformation.ProductName' : "Product-1" }, { $set : { 'ProductInformation.$.ProductPrice' : 1250}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }Display ... Read More

MongoDB query to filter object where all elements from nested array match the condition

AmitDiwan
Updated on 03-Apr-2020 13:40:35

637 Views

For this, use aggregate(). Let us first create a collection with documents −> db.demo418.insertOne( ...    { ...       "details":[ ...          { ...             "CountryName":"US", ...             "Marks":45 ...          }, ...          { ...             "CountryName":"US", ...             "Marks":56 ...          }, ... ...       ], ...       "Name":"John" ...    } ... ); {    "acknowledged" : true,   ... Read More

Update salary field value with 10 percent of each employee in MongoDB

AmitDiwan
Updated on 03-Apr-2020 13:38:00

810 Views

Let us first create a collection with documents −> db.demo417.insertOne({"EmployeeName":"Chris", "EmployeeSalary":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723ebbb912067e57771ae4") } > db.demo417.insertOne({"EmployeeName":"Mike", "EmployeeSalary":1000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723ed7b912067e57771ae5") }Display all documents from a collection with the help of find() method −> db.demo417.find();This will produce the following output −{ "_id" : ObjectId("5e723ebbb912067e57771ae4"), "EmployeeName" : "Chris", "EmployeeSalary" : 500 } { "_id" : ObjectId("5e723ed7b912067e57771ae5"), "EmployeeName" : "Mike", "EmployeeSalary" : 1000 }Following is the query to update salary field value with 10 percent of each employee in employee collection −> db.demo417.update({ "EmployeeName": { $in: ["Chris", "Mike"] } }, ... Read More

Convert Collection to Capped in MongoDB

AmitDiwan
Updated on 03-Apr-2020 13:35:55

170 Views

To convert collection to capped, use runCommand(). It provides a helper to run specified database commands. Let us first create a collection with documents −> db.demo416.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723a7bb912067e57771adf") } > db.demo416.insertOne({"StudentName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723a81b912067e57771ae0") } > db.demo416.insertOne({"StudentName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723a84b912067e57771ae1") }Display all documents from a collection with the help of find() method −> db.demo416.find();This will produce the following output −{ "_id" : ObjectId("5e723a7bb912067e57771adf"), "StudentName" : "David" } { "_id" : ObjectId("5e723a81b912067e57771ae0"), "StudentName" : "Mike" } { "_id" : ObjectId("5e723a84b912067e57771ae1"), "StudentName" : ... Read More

How to select specific columns in MongoDB query?

AmitDiwan
Updated on 03-Apr-2020 13:28:16

7K+ Views

To select specific columns, you can ignore the rest of them i.e. to hide those columns, set them to 0. Let us first create a collection with documents −> db.demo415.insertOne({"ClientName":"Robert", "ClientCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e72329db912067e57771adc") } > db.demo415.insertOne({"ClientName":"David", "ClientCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7232acb912067e57771add") } > db.demo415.insertOne({"ClientName":"Bob", "ClientCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7232b4b912067e57771ade") }Display all documents from a collection with the help of find() method −> db.demo415.find();This will produce the following output −{ "_id" : ObjectId("5e72329db912067e57771adc"), "ClientName" : "Robert", "ClientCountryName" : "US" } { "_id" : ObjectId("5e7232acb912067e57771add"), ... Read More

Find by _id on an array of objects in MongoDB database?

AmitDiwan
Updated on 03-Apr-2020 13:26:08

697 Views

To find by _id on an array of objects, use aggregation and avoid using find(). Let us first create a collection with documents −> db.demo414.insertOne( ...    { ...       "_id": "110", ...       "details":[ ...          { ...             "StudentName":"John", ...             "StudentMarks":56 ...          }, ...          { ...             "StudentName":"Robert", ...             "StudentMarks":98 ...          } ...       ] ...    } ... ); { "acknowledged" : true, "insertedId" : "110" }Display all documents from a collection with the help of find() method −> db.demo414.find();This will produce the following output −{ "_id" : "110", "details" : [ { "StudentName" : "John", "StudentMarks" : 56 }, { "StudentName" : "Robert", "StudentMarks" : 98 } ] }Following is the query to find by _id on an array of objects −> db.demo414.aggregate([{$unwind: "$details"}, {$match:{"details.StudentMarks" :56}}] )This will produce the following output −{ "_id" : "110", "details" : { "StudentName" : "John", "StudentMarks" : 56 } }

Is it possible to exclude nested fields in MongoDB with a wildcard?

AmitDiwan
Updated on 03-Apr-2020 13:24:14

748 Views

Achieve this with aggregation pipeline. Let us first create a collection with documents −> db.demo413.insertOne( ...    { ...       "_id": "101", ...       "details": { ...          "Info1": { ...             Name:"Chris", ...             Age:21 ...          }, ...          "Info2": { ...             Name:"David", ...             Age:23 ...          } ...       } ...    } ... ); { "acknowledged" : true, "insertedId" : "101" }Display all documents from a collection with the help of find() method −> db.demo413.find();This will produce the following output −{ "_id" : "101", "details" : { "Info1" : { "Name" : "Chris", "Age" : 21 }, "Info2" : { "Name" : "David", "Age" : 23 } } }Following is the query to exclude nested fields −> db.demo413.aggregate([ ...    { $project: { "details" : { $objectToArray: "$details" } } }, ...    { $project: { "details.v.Age" : 0} }, ...    { $project: { "details" : { $arrayToObject: "$details"} } } ... ]);This will produce the following output −{ "_id" : "101", "details" : { "Info1" : { "Name" : "Chris" }, "Info2" : { "Name" : "David" } } }

Update an array of strings nested within an array of objects in MongoDB

AmitDiwan
Updated on 03-Apr-2020 13:22:52

204 Views

Let us create a collection with documents −> db.demo411.aggregate( ...    [ ...       {$project : { ...          _id : 0, ...          Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}} ...          } ...       } ...    ] ... ) { "Information" : [ [ "Chris", "David" ], [ "John", "John" ] ] } > db.demo412.insertOne( ...    { ...       "Information1" : [ ...          { ...           ... Read More

Advertisements