Add a Deep Nested Array of Objects Using $addToSet in MongoDB

AmitDiwan
Updated on 02-Apr-2020 13:32:30

1K+ Views

The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us first create a collection with documents −> db.demo380.insertOne({ ... ...    "details" : [ ...       { ...          "Name" : "Chris", ...          "details1" : [ ] ...       }, ...       { ...          "Name" : "David", ...          "details1" : [ ] ...       } ...    ] ... } ... ); ... Read More

MongoDB Projection on Specific Nested Properties

AmitDiwan
Updated on 02-Apr-2020 13:29:06

1K+ Views

For projection on specific nested properties, use aggregate() in MongoDB. Let us first create a collection with documents −> db.demo379.insertOne( ...    { ...       "details1" : { ...          "details2" : { ...             "details3" : { ...                "10" : "John", ...                "50" : "Chris", ...                "40" : "David", ...                "30":"Mike" ...             } ...   ... Read More

Manipulating Subdocuments in MongoDB

AmitDiwan
Updated on 02-Apr-2020 13:25:17

180 Views

To manipulate subdocuments, use dot(.) notation in MongoDB. Let us first create a collection with documents −> db.demo378.insertOne( ...    { ...       Name: 'Chris', ...       details:[ ...          {id:101, Score:56}, ...          {id:102, Score:78} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5a758a2ae06a1609a00b0f") }Display all documents from a collection with the help of find() method −> db.demo378.find();This will produce the following output −{    "_id" : ObjectId("5e5a758a2ae06a1609a00b0f"), "Name" : "Chris", "details" : [       { "id" ... Read More

Concatenate Array of Integers in MongoDB Aggregation Method

AmitDiwan
Updated on 02-Apr-2020 13:21:25

428 Views

To concatenate, use $concat in MongoDB aggregate(). Let us first create a collection with documents −> db.demo377.insertOne({"ListOfIds":[1001, 1002, 1003, 1004, 1005, 1006, 1007]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5a73462ae06a1609a00b0e") }Display all documents from a collection with the help of find() method −> db.demo377.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e5a73462ae06a1609a00b0e"),    "ListOfIds" : [       1001,       1002,       1003,       1004,       1005,       1006,       1007    ] }Following is the query to concatenate an array of integer ... Read More

Update After Aggregate in MongoDB

AmitDiwan
Updated on 02-Apr-2020 13:18:50

643 Views

To update documents, you cannot use aggregation pipeline. You can use update(). Let us first create a collection with documents −> db.demo376.insertOne( ...    { ... ...       "id" :101, ... ...       "details" : [ ...          { ...             Name:"Chris", ...             Age:21, ...             Score:45 ...          }, ...          { ...             Name:"David", ...             Age:23, ...   ... Read More

MongoDB Query to Execute Stored Function

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

565 Views

JavaScript function can be saved for reuse using a system collection called system.js. To store a function, use the db.collection.save(),Let us first create a function. Following is the query −> db.system.js.save({ ...    _id: "displayMessage", ...    value: function (data) { ...       return 'The Name is: ' + data; ...    } ... })This will produce the following output −WriteResult({    "nMatched" : 0,    "nUpserted" : 1,    "nModified" : 0,    "_id" : "displayMessage" })Following is the query to execute stored function −> db.eval("displayMessage('John')") WARNING: db.eval is deprecatedThis will produce the following output −The Name is: John

Difference Between show dbs and show databases in MongoDB

AmitDiwan
Updated on 02-Apr-2020 13:13:04

305 Views

There is no difference between show dbs and show databases. Both commands internally call listDatabases command.The show dbs command is as follows −> show dbsThis will produce the following output −admin             0.002GB app                 0.000GB business           0.000GB config             0.000GB local             0.000GB main             0.000GB ... Read More

Find Documents When Keys are Unknown in MongoDB

AmitDiwan
Updated on 02-Apr-2020 13:08:30

694 Views

To find when the keys are unknown, use $addField and $objectToArray. Let us first create a collection with documents −> db.demo375.insertOne( ...    { ...       "details":{ ...          "Name":"John", ...          "Age":23 ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5a0ae42ae06a1609a00b06") } > db.demo375.insertOne( ...    { ...       "details":{ ...          "Name":"David", ...          "Age":21 ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" ... Read More

Find Values Group by Another Field in MongoDB

AmitDiwan
Updated on 02-Apr-2020 13:04:56

470 Views

To group by another field, use $group along with $project. Let us first create a collection with documents −> db.demo374.insertOne( ...    { ... ...       "Name" : "Chris", ...       "HobbyDetails" : [ ...          "Reading Book", ...          "Playing Football" ...       ], ...       "CountryName" : "US" ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5a04402ae06a1609a00b04") } > db.demo374.insertOne( ...    { ... ...       "Name" : "Chris", ...       "HobbyDetails" : [ ... ... Read More

Index and Sort with Pagination using Custom Field in MongoDB

AmitDiwan
Updated on 02-Apr-2020 13:02:12

402 Views

Let us first create a collection with documents −> db.demo373.createIndex({"Name":1, "CountryName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo373.insertOne({"Name":"Chris", "Age":22, "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e59ffde2ae06a1609a00aff") } > db.demo373.insertOne({"Name":"David", "Age":21, "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e59ffe82ae06a1609a00b00") } > db.demo373.insertOne({"Name":"Bob", "Age":23, "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e59fff42ae06a1609a00b01") } > db.demo373.insertOne({"Name":"John", "Age":21, "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e59ffff2ae06a1609a00b02") } > db.demo373.insertOne({"Name":"Carol", "Age":23, "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5a00082ae06a1609a00b03") }Display all ... Read More

Advertisements