AmitDiwan has Published 10744 Articles

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

AmitDiwan

AmitDiwan

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

782 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" : ... Read More

How to get data of array intersection in MongoDB?

AmitDiwan

AmitDiwan

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

514 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") ... Read More

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

AmitDiwan

AmitDiwan

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

85 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", ... Read More

MongoDB Aggregate Second Element from Input Element?

AmitDiwan

AmitDiwan

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

110 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, ... Read More

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

AmitDiwan

AmitDiwan

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

940 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") } ... Read More

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

AmitDiwan

AmitDiwan

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

162 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") ... Read More

Group query upon nested object in MongoDB?

AmitDiwan

AmitDiwan

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

298 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 ... Read More

How can I aggregate collection and group by field count in MongoDB?

AmitDiwan

AmitDiwan

Updated on 12-May-2020 06:43:28

661 Views

In MongoDB aggregate(), use $group and aggregate collection. Let us create a collection with documents −> db.demo616.insertOne({"details":{"Name":"Chris", "Age":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfac65492f6c60d00283") } > db.demo616.insertOne({"details":{"Name":"Chris", "Age":22}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfb065492f6c60d00284") } > db.demo616.insertOne({"details":{"Name":"Bob", "Age":23}}); {    "acknowledged" : true,   ... Read More

How to use $type in MongoDB?

AmitDiwan

AmitDiwan

Updated on 12-May-2020 06:41:09

125 Views

The $type selects documents where the value of the field is an instance of the specified BSON type. Let us create a collection with documents −> db.demo615.insert({"Value":100}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":"100"}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":"300"}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":300}); WriteResult({ "nInserted" ... Read More

What is ({$natural: 1}) in MongoDB?

AmitDiwan

AmitDiwan

Updated on 12-May-2020 06:40:05

1K+ Views

The ({$natural − 1}) works like LIFO(LAST IN FIRST OUT), that means last inserted document will be shown first.Let us create a collection with documents −> db.demo614.insertOne({"CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988cddf6b89257f5584d8e") } > db.demo614.insertOne({"CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988ce0f6b89257f5584d8f") } > ... Read More

Advertisements