Found 1659 Articles for Big Data Analytics

Sum with MongoDB group by multiple columns to calculate total marks with duplicate ids

AmitDiwan
Updated on 12-May-2020 07:11:54

693 Views

For this, use aggregate() along with $group. Let us create a collection with documents −> db.demo627.insertOne({id:101, "Name":"Chris", "Marks":54}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9acb306c954c74be91e6b2") } > db.demo627.insertOne({id:102, "Name":"Bob", "Marks":74}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9acb3c6c954c74be91e6b3") } > db.demo627.insertOne({id:101, "Name":"Chris", "Marks":87}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9acb426c954c74be91e6b4") } > db.demo627.insertOne({id:102, "Name":"Mike", "Marks":70}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9acb4b6c954c74be91e6b5") }Display all documents from a collection with the help of find() method −> db.demo627.find();This will produce the following output −{ "_id" : ObjectId("5e9acb306c954c74be91e6b2"), "id" : 101, "Name" : "Chris", "Marks" : ... Read More

Remove document whose value is matched with $eq from a MongoDB collection?

AmitDiwan
Updated on 12-May-2020 14:52:51

241 Views

Remove document using remove(), whose value is matched with $eq from a MongoDB collection. The $eq operator matches documents where the value of a field equals the specified value.Let us create a collection with documents −> db.demo626.insertOne({id:1, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac6376c954c74be91e6ae") } > db.demo626.insertOne({id:2, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac63e6c954c74be91e6af") } > db.demo626.insertOne({id:3, "Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac6436c954c74be91e6b0") } > db.demo626.insertOne({id:4, "Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac6486c954c74be91e6b1") }Display all documents from a collection with the help of find() method −> db.demo626.find();This ... Read More

How to get data of array intersection in MongoDB?

AmitDiwan
Updated on 12-May-2020 07:07:10

389 Views

For array interection in MongoDB, use the $setIntersection in aggregate(). Let us create a collection with documents −> db.demo625.insertOne( ...    { ...       Name: "John", ...       Marks: [56, 98, 60] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab8e16c954c74be91e6aa") } > db.demo625.insertOne( ...    { ...       Name: "John", ...       Marks: [110, 56, 72] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab8e26c954c74be91e6ab") } > db.demo625.insertOne( ...    { ...       Name: "Chris", ...       ... Read More

MongoDB - How can I see if all elements of a field are contained in a superset?

AmitDiwan
Updated on 12-May-2020 14:59:26

80 Views

For all elements of a field in MongoDB, use find() and in that, use $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.Let us create a collection with documents −> db.demo624.insertOne({"ListOfName":["John", "Chris", "David", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab3ff6c954c74be91e6a5") } > db.demo624.insertOne({"ListOfName":["John", "Chris"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab4026c954c74be91e6a6") } > db.demo624.insertOne({"ListOfName":["John", "Chris", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab4076c954c74be91e6a7") } > db.demo624.insertOne({"ListOfName":["John", "Chris", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab40e6c954c74be91e6a8") } > ... Read More

MongoDB Indexes - Is it possible to create both normal & compound at the same time?

AmitDiwan
Updated on 12-May-2020 06:59:31

47 Views

Yes, you can use ensureIndex(). MongoDB provides complete support for indexes on any field in a collection of documents.Let us create a collection with documents −> db.demo622.ensureIndex({_id:1, Name:1, Age:1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo622.insertOne({_id:101, Name:"Chris", Age:21}); { "acknowledged" : true, "insertedId" : 101 } > db.demo622.insertOne({_id:102, Name:"Chris", Age:22}); { "acknowledged" : true, "insertedId" : 102 } > db.demo622.insertOne({_id:103, Name:"Bob", Age:21}); { "acknowledged" : true, "insertedId" : 103 } > db.demo622.insertOne({_id:104, Name:"Chris", Age:22}); { "acknowledged" : true, "insertedId" : 104 } > db.demo622.insertOne({_id:104, Name:"Chris", Age:22}); 2020-04-18T12:21:18.085+0530 ... Read More

MongoDB Aggregate Second Element from Input Element?

AmitDiwan
Updated on 12-May-2020 06:57:19

73 Views

To aggregate second element from input element, use mapReduce(). Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results. Let us create a collection with documents −> db.demo621.insert({ _id: 101, Name1: "John", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 102, Name1: "Bob", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 103, Name1: "Chris", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 104, Name1: "Sam", Name2: "John" }); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> ... Read More

Aggregate by country, state and city in a MongoDB collection with multiple documents

AmitDiwan
Updated on 12-May-2020 06:54:24

757 Views

Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result.To aggregate in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo620.insertOne({"Country":"IND", "City":"Delhi", state:"Delhi"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9a8de96c954c74be91e6a1") } > db.demo620.insertOne({"Country":"IND", "City":"Bangalore", state:"Karnataka"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9a8e336c954c74be91e6a3") } > db.demo620.insertOne({"Country":"IND", "City":"Mumbai", state:"Maharashtra"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9a8e636c954c74be91e6a4") }Display all documents from a collection with the help of find() method −> db.demo620.find();This will produce the following output −{ "_id" : ObjectId("5e9a8de96c954c74be91e6a1"), ... Read More

MongoDB – Fix “Failed to convert from type String to type Date”?

AmitDiwan
Updated on 12-May-2020 14:57:13

393 Views

To fix this, use $dateFromString in MongoDB aggregate(). The $dateFromString converts a date/time string to a date object.Let us create a collection with documents −> db.demo619.insertOne({"DueDate":"10-10-2020"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7846c954c74be91e69e") } > db.demo619.insertOne({"DueDate":"12-01-2019"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7996c954c74be91e69f") } > db.demo619.insertOne({"DueDate":"28-10-2010"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7ab6c954c74be91e6a0") }Display all documents from a collection with the help of find() method −> db.demo619.find();This will produce the following output −{ "_id" : ObjectId("5e99d7846c954c74be91e69e"), "DueDate" : "10-10-2020" } { "_id" : ObjectId("5e99d7996c954c74be91e69f"), "DueDate" : "12-01-2019" } { "_id" : ObjectId("5e99d7ab6c954c74be91e6a0"), "DueDate" : ... Read More

Can we work MongoDB findOne() with long type _id?

AmitDiwan
Updated on 12-May-2020 06:47:03

119 Views

Yes, we can do that using the NumberLong() datatype in MongoDB. Let us create a collection with documents −> db.demo618.insertOne({_id:NumberLong("6336366454"), Name:"Chris"}); { "acknowledged" : true, "insertedId" : NumberLong("6336366454") } > db.demo618.insertOne({_id:NumberLong("6336366455"), Name:"David"}); { "acknowledged" : true, "insertedId" : NumberLong("6336366455") } > db.demo618.insertOne({_id:NumberLong("6336366456"), Name:"Bob"}); { "acknowledged" : true, "insertedId" : NumberLong("6336366456") }Display all documents from a collection with the help of find() method −> db.demo618.find();This will produce the following output −{ "_id" : NumberLong("6336366454"), "Name" : "Chris" } { "_id" : NumberLong("6336366455"), "Name" : "David" } { "_id" : NumberLong("6336366456"), "Name" : "Bob" }Following is the query to implement MongoDB findOne() ... Read More

Group query upon nested object in MongoDB?

AmitDiwan
Updated on 12-May-2020 06:45:23

231 Views

For this, use dot notation along with $group in MongoDB. Let us create a collection with documents −> db.demo617.insertOne( ...    { ... ...       "clientDetails": { ...          "Name": "Chris", ...          "Age":32, ...          "Project":"Online Library Management System" ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d2b86c954c74be91e69b") } > > db.demo617.insertOne( ...    { ... ...       "clientDetails": { ...          "Name": "David", ...          "Age":34, ...     ... Read More

Advertisements