Found 1359 Articles for MongoDB

MongoDB query to display all the fields value, except _id

AmitDiwan
Updated on 15-May-2020 06:50:45

313 Views

Let us create a collection with documents −> db.demo590.insert([ ...    { "Name": "Chris", "Age": 21 }, ...    {"Name": "Bob", "Age": 20}, ...    { "Name": "Sam", "Age": 19 } ... ]); BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 3,    "nUpserted" : 0,    "nMatched" : 0,    "nModified" : 0,    "nRemoved" : 0,    "upserted" : [ ] })Display all documents from a collection with the help of find() method −> db.demo590.find();This will produce the following output −{ "_id" : ObjectId("5e92d514fd2d90c177b5bcd0"), "Name" : "Chris", "Age" : 21 } { ... Read More

MongoDB query to find “where” Billing Address is Equal to Delivery Address from documents?

AmitDiwan
Updated on 15-May-2020 06:48:31

109 Views

To check equality and fetch the documents, use $where in MongoDB. Let us create a collection with documents −> db.demo589.insertOne({deliveryAddress:"US", billingAddress:"UK"});{    "acknowledged" : true, "insertedId" : ObjectId("5e92c117fd2d90c177b5bccc") } > db.demo589.insertOne({deliveryAddress:"US", billingAddress:"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e92c11bfd2d90c177b5bccd") } > db.demo589.insertOne({deliveryAddress:"US", billingAddress:"AUS"});{    "acknowledged" : true, "insertedId" : ObjectId("5e92c11ffd2d90c177b5bcce") } > db.demo589.insertOne({deliveryAddress:"UK", billingAddress:"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e92c127fd2d90c177b5bccf") }Display all documents from a collection with the help of find() method −> db.demo589.find();This will produce the following output −{ "_id" : ObjectId("5e92c117fd2d90c177b5bccc"), "deliveryAddress" : "US", "billingAddress" : "UK" } { "_id" : ObjectId("5e92c11bfd2d90c177b5bccd"), "deliveryAddress" : "US", "billingAddress" : ... Read More

MongoDB query to gather unique array item?

AmitDiwan
Updated on 15-May-2020 06:46:31

187 Views

To gather a unique array items, use distinct(). Let us create a collection with documents −> db.demo588.insertOne({"CountryName":["US","AUS","UK","US","UK","AUS"]});{    "acknowledged" : true, "insertedId" : ObjectId("5e92bbd2fd2d90c177b5bccb") }Display all documents from a collection with the help of find() method −> db.demo588.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e92bbd2fd2d90c177b5bccb"),    "CountryName" : [       "US",       "AUS",       "UK",       "US",       "UK",       "AUS"    ] }Following is the query to gather unique array item −> db.demo588.distinct("CountryName");This will produce the following output −[ "AUS", "UK", "US" ]

How to keep appending subdocuments in MongoDB?

AmitDiwan
Updated on 15-May-2020 06:45:17

94 Views

To append subdocuments, use $push in MongoDB. The update() is used to update. Let us create a collection with documents −> db.demo587.insertOne({"id":101, "details":[{Name:"Chris", Age:21, Marks:57}]});{    "acknowledged" : true, "insertedId" : ObjectId("5e92ba01fd2d90c177b5bcc9") } > db.demo587.insertOne({"id":102, "details":[{Name:"Bob", Age:22, Marks:78}]});{    "acknowledged" : true, "insertedId" : ObjectId("5e92ba0efd2d90c177b5bcca") }Display all documents from a collection with the help of find() method −> db.demo587.find();This will produce the following output −{ "_id" : ObjectId("5e92ba01fd2d90c177b5bcc9"), "id" : 101, "details" : [ { "Name" : "Chris", "Age" : 21, "Marks" : 57 } ] } { "_id" : ObjectId("5e92ba0efd2d90c177b5bcca"), "id" : 102, "details" : [ { "Name" : ... Read More

MongoDB query to select distinct and count?

AmitDiwan
Updated on 15-May-2020 06:43:20

818 Views

Let us create a collection with documents −> db.demo586.insertOne( ...    {"details": [ ...       { ...          "Name":"Chris", ...          "Marks":71 ...       }, ...       { ...          "Name":"Chris", ...          "Marks":61 ...       }, ...       { ...          "Name":"David", ...          "Marks":81 ...       } ...   ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9200fefd2d90c177b5bcc7") } > db.demo586.insertOne( ... Read More

MongoDB: Using reference as key and manually adding a value?

AmitDiwan
Updated on 15-May-2020 06:39:17

174 Views

To manually add value, use $push in MongoDB. Let us create a collection with documents −> db.demo585.insert({ ...    firstName: 'John', ...    lastName: 'Doe', ...    SubjectName:"MongoDB", ...    Marks: [59] ... }); WriteResult({ "nInserted" : 1 }) > db.demo585.insert({ ...    firstName: 'Chris', ...    lastName: 'Brown', ...    SubjectName:"MySQL", ...    Marks: [79] ... }); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method−> db.demo585.find();This will produce the following output −{ "_id" : ObjectId("5e91fd80fd2d90c177b5bcc3"), "firstName" : "John", "lastName" : "Doe", "SubjectName" : "MongoDB", "Marks" : [ 59 ] } { ... Read More

How to project grouping into object in MongoDB and display only the marks field?

AmitDiwan
Updated on 15-May-2020 06:36:46

62 Views

Let us first create a document −> var document= [ ...    { "SubjectName" : "MySQL", "Marks" : 78 }, ...    { "SubjectName" : "MongoDB", "Marks" : 89 }, ...    { "SubjectName" : "Java", "Marks" : 71 }, ... ];Following is the query to display document −> printjson(document);This will produce the following output −[    {       "SubjectName" : "MySQL",       "Marks" : 78    },    {       "SubjectName" : "MongoDB",       "Marks" : 89    },    {       "SubjectName" : "Java",       "Marks" : 71    } ]Following is the query to project grouping into an object in MongoDB −> var makeObject= {}; > document.forEach(function (d){ ...    makeObject[d.SubjectName] = d.Marks; ... }); > printjson(makeObject);This will produce the following output −{ "MySQL" : 78, "MongoDB" : 89, "Java" : 71 }

MongoDB query to get average in aggregation of array element?

AmitDiwan
Updated on 15-May-2020 06:29:52

772 Views

To get an average of array elements, use $avg. Let us create a collection with documents −> db.demo584.insertOne({"Marks":[75,50,85,60,80]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e91d827fd2d90c177b5bcc2") }Display all documents from a collection with the help of find() method −> db.demo584.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e91d827fd2d90c177b5bcc2"),    "Marks" : [       75,       50,       85,       60,       80    ] }Following is the query to find avg in the aggregation of array element −> db.demo584.aggregate([ ...    { $project: { MarksAvg: { $avg: "$Marks"} } } ... ])This will produce the following output −{ "_id" : ObjectId("5e91d827fd2d90c177b5bcc2"), "MarksAvg" : 70 }

Filter sub documents by sub document in MongoDB?

AmitDiwan
Updated on 15-May-2020 06:28:34

498 Views

For this, use aggregate() along with $unwind. Let us create a collection with documents −> db.demo583.insert([ ...    { ...       "details1" : [ ...          { ...             "details2" : [ ...                { ...                   "isMarried" : true, ...                   "Name" : "Chris" ...                }, ...                { ...         ... Read More

Apply the Group Accumulator Operator $first on the system variable $$ROOT to return reference to the root document?

AmitDiwan
Updated on 15-May-2020 06:23:46

97 Views

The accumulators are operators that maintain their state as documents progress through the pipeline.The $ROOT references the root document, i.e. the top-level document, currently being processed in the aggregation pipeline stage.Let us create a collection with documents −> db.demo582.insertOne({FirstName:"Chris", Age:21, createDate:new ISODate("2020-01-10")});{    "acknowledged" : true, "insertedId" : ObjectId("5e91ce41fd2d90c177b5bcbd") } > db.demo582.insertOne({FirstName:"Chris", Age:21, createDate:new ISODate("2020-04-21")});{    "acknowledged" : true, "insertedId" : ObjectId("5e91ce4ffd2d90c177b5bcbe") } > db.demo582.insertOne({FirstName:"Chris", Age:22, createDate:new ISODate("2020-02-11")});{    "acknowledged" : true, "insertedId" : ObjectId("5e91ce59fd2d90c177b5bcbf") } > db.demo582.insertOne({FirstName:"Chris", Age:22, createDate:new ISODate("2020-01-12")});{    "acknowledged" : true, "insertedId" : ObjectId("5e91ce6efd2d90c177b5bcc0") }Display all documents from a collection with the help of find() method ... Read More

Advertisements