Found 1659 Articles for Big Data Analytics

MongoDB query to aggregate nested array

AmitDiwan
Updated on 06-Apr-2020 13:25:14

959 Views

To aggregate nested array in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo441.insertOne( ...    { ... ...       "Name" : "David", ...       "Age" : 21, ... ...       "details" : [ ...          { ...             "id" : 1, ...             "CountryName" : "US", ...             "details1" : [ ...                { ...                   "SubjectName" : ... Read More

How to count items in array with MongoDB?

AmitDiwan
Updated on 06-Apr-2020 13:17:15

212 Views

To count items in array, use length. Let us create a collection with documents −> db.demo440.insertOne( ...    { ...       "Name":"Chris", ...       "ListOfFriends":["John", "Sam", "Mike"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78c63cbbc41e36cc3caeb5") } > > db.demo440.insertOne( ...    { ...       "Name":"David", ...       "ListOfFriends":["Mike", "Bob", "Carol"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78c63cbbc41e36cc3caeb6") }Display all documents from a collection with the help of find() method −> db.demo440.find();This will produce the following output −{ "_id" : ... Read More

Project field in MongoDB

AmitDiwan
Updated on 06-Apr-2020 13:14:17

158 Views

To project field in MongoDB, use $project. Let us create a collection with documents −> db.demo439.insertOne( ...    { ...       "Name" : "Chris", ...       "MarksInformation" : { ...          "Marks1" : 67, ...          "Marks2" :45, ...          "Marks3" : 78 ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e77833abbc41e36cc3caeab") } > db.demo439.insertOne( ...    { ...       "Name" : "David", ...       "MarksInformation" : { ...         ... Read More

How to remove duplicate record in MongoDB 3.x?

AmitDiwan
Updated on 06-Apr-2020 13:08:14

262 Views

To remove duplicate record, use aggregate(). Let us create a collection with documents −> db.demo438.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e775c37bbc41e36cc3caea1") } > db.demo438.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e775c3dbbc41e36cc3caea2") } > db.demo438.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e775c40bbc41e36cc3caea3") } > db.demo438.insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e775c44bbc41e36cc3caea4") } > db.demo438.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e775c47bbc41e36cc3caea5") }Display all documents from a collection with the help of find() method −> db.demo438.find();This will produce the following output −{ "_id" : ObjectId("5e775c37bbc41e36cc3caea1"), "FirstName" : "Chris" } { ... Read More

Perform simple validation in MongoDB?

AmitDiwan
Updated on 06-Apr-2020 14:09:04

188 Views

For validation in MongoDB, use validator. Following is the query to create validation on collection in MongoDB −> db.createCollection( "demo437" , { ...    validator: { $jsonSchema: { ...       bsonType: "object", ...       required: [ "FirstName", "LastName"], ...       properties: { ...          FirstName: { ...             bsonType: "string", ...             description: "This is required" }, ...             LastName: { ...                bsonType: "string", ...         ... Read More

How to merge multiple documents in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 14:22:58

1K+ Views

To merge multiple documents in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo436.insertOne( ...    { ...       "_id" : "101", ...       "Name": "Chris", ...       "details" : [ ...          { ...             "CountryName" : "US", ...             "Age" : 21 ...          } ...       ], ...       "Price" : 50 ...    } ... ); { "acknowledged" : true, "insertedId" : "101" } > db.demo436.insertOne( ... ... Read More

How to append to array in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 14:20:41

259 Views

To append to array in MongoDB, use $concatArrays. Let us create a collection with documents −> db.demo435.insertOne({"FirstName":["Chris"], "LastName":["Brown"]} ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7719b1bbc41e36cc3cae97") } > db.demo435.insertOne({"FirstName":["David"], "LastName":["Miller"]} ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7719bdbbc41e36cc3cae98") } > db.demo435.insertOne({"FirstName":["John"], "LastName":["Doe"]} ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7719c6bbc41e36cc3cae99") }Display all documents from a collection with the help of find() method −> db.demo435.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e7719b1bbc41e36cc3cae97"),    "FirstName" : [       "Chris"       ],       "LastName" : [     ... Read More

MongoDB aggregation / math operation to sum score of a specific student

AmitDiwan
Updated on 03-Apr-2020 14:18:50

197 Views

To sum, use aggregate() along with $sum. Let us create a collection with documents −> db.demo434.insertOne({"Name":"Chris", "Score":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e771603bbc41e36cc3cae93") } > db.demo434.insertOne({"Name":"David", "Score":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e77161abbc41e36cc3cae94") } > db.demo434.insertOne({"Name":"Chris", "Score":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e771624bbc41e36cc3cae95") }Display all documents from a collection with the help of find() method −> db.demo434.find();This will produce the following output −{ "_id" : ObjectId("5e771603bbc41e36cc3cae93"), "Name" : "Chris", "Score" : 45 } { "_id" : ObjectId("5e77161abbc41e36cc3cae94"), "Name" : "David", "Score" : 55 } { "_id" : ObjectId("5e771624bbc41e36cc3cae95"), "Name" : "Chris", ... Read More

How to filter a query on specific date format with MongoDB?

AmitDiwan
Updated on 03-Apr-2020 14:17:09

2K+ Views

To filter a query on specific date format, use $dateToString. Let us create a collection with documents −> db.demo433.insertOne({"DueDate":new Date("2019-11-23")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e771278bbc41e36cc3cae91") } > db.demo433.insertOne({"DueDate":new Date("2020-01-03")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e771290bbc41e36cc3cae92") }Display all documents from a collection with the help of find() method −> db.demo433.find();This will produce the following output −{ "_id" : ObjectId("5e771278bbc41e36cc3cae91"), "DueDate" : ISODate("2019-11-23T00:00:00Z") } { "_id" : ObjectId("5e771290bbc41e36cc3cae92"), "DueDate" : ISODate("2020-01-03T00:00:00Z") }Following is the query to filter a query on specific date format in MongoDB −> db.demo433.aggregate([ ... { $addFields: {stringDate: { $dateToString: { format: ... Read More

Aggregation framework to get the name of students with test one score less than the total average of all the tests

AmitDiwan
Updated on 03-Apr-2020 14:15:06

87 Views

For this, you can use aggregate(). We have considered test records as “Value1”, “Value2”, etc. Let us create a collection with documents −> db.demo432.insertOne( ...    { ...       "_id" : 101, ...       "Name" : "David", ...       "Value1" : 67, ...       "Value2" : 87, ...       "Value3" : 78 ...    } ... ) { "acknowledged" : true, "insertedId" : 101 } > db.demo432.insertOne( ...    { ...       "_id" : 102, ...       "Name" : "Sam", ...       "Value1" : ... Read More

Advertisements