Remove Values from a Matrix-like Document in MongoDB

AmitDiwan
Updated on 12-May-2020 07:33:27

174 Views

To remove values from a matrix, use $pull in MongoDB. Let us create a collection with documents −> db.demo632.insertOne( ...    { ...       "arrayMatrix": [ ...          [10, 20], ...          [10, 20], ...          [10, 20], ...          [10, 20], ...          [10, 20], ...          [10, 20] ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9b27b46c954c74be91e6c0") }Display all documents from a collection with the help of find() ... Read More

Return Specific MongoDB Embedded Document

AmitDiwan
Updated on 12-May-2020 07:23:41

244 Views

Use $unwind twice for specific embedded document in MongoDB. Let us create a collection with documents −> db.demo631.insert( ...    { ...       id: "101", ...       Info1: [ ...          { ...             CountryName : "US", ...             Info2 : [ ...                { ...                   Name:"Chris", ...                   Age:24 ...                }, { ... Read More

Query for Values Not Objects in List with MongoDB

AmitDiwan
Updated on 12-May-2020 07:13:29

170 Views

To query for values in list, use positional operator($) in MongoDB. Let us create a collection with documents −> db.demo628.insertOne({id:1, Name:["Chris", "David", "John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ae7ea6c954c74be91e6b6") } > db.demo628.insertOne({id:1, Name:["Carol", "Sam"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ae7f26c954c74be91e6b7") } > db.demo628.insertOne({id:2, Name:["Mike", "Sam", "John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ae8056c954c74be91e6b8") }Display all documents from a collection with the help of find() method −> db.demo628.find();This will produce the following output −{ "_id" : ObjectId("5e9ae7ea6c954c74be91e6b6"), "id" : 1, "Name" : [ "Chris", "David", "John" ] } { "_id" : ObjectId("5e9ae7f26c954c74be91e6b7"), "id" ... Read More

Sum with MongoDB Group By Multiple Columns to Calculate Total Marks

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

836 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

Get Data of Array Intersection in MongoDB

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

552 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

Create Normal and Compound Indexes in MongoDB

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

121 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

Aggregate Second Element from Input Element in MongoDB

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

137 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 MongoDB Collection

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

984 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

Using findOne with Long Type ID in MongoDB

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

187 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

336 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