AmitDiwan has Published 10744 Articles

MongoDB query to match documents that contain an array field

AmitDiwan

AmitDiwan

Updated on 15-May-2020 06:54:47

261 Views

To match documents that contain an array field, use the $elemMatch operator. Let us create a collection with documents −> db.demo592.insertOne( ...    { ...       "id":101, ...       "details" : [ ...          { "Name" : "Chris", "Value" : "200"}, ...   ... Read More

MongoDB find() query for nested document?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 06:52:22

687 Views

To fetch a value from the nested document, use dot notation. Let us create a collection with documents −> db.demo591.insert([ ...    { "Name": "John", "Age": 23 }, ...    {"Name": "Carol", "Age": 26}, ...    { "Name": "Robert", "Age": 29, ...    details:[ ...       { ... ... Read More

MongoDB query to display all the fields value, except _id

AmitDiwan

AmitDiwan

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

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

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

AmitDiwan

AmitDiwan

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

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

MongoDB query to gather unique array item?

AmitDiwan

AmitDiwan

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

225 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 −{   ... Read More

How to keep appending subdocuments in MongoDB?

AmitDiwan

AmitDiwan

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

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

MongoDB query to select distinct and count?

AmitDiwan

AmitDiwan

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

1K+ Views

Let us create a collection with documents −> db.demo586.insertOne( ...    {"details": [ ...       { ...          "Name":"Chris", ...          "Marks":71 ...       }, ...       { ...          "Name":"Chris", ...       ... Read More

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

AmitDiwan

AmitDiwan

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

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

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

AmitDiwan

AmitDiwan

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

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

MongoDB query to get average in aggregation of array element?

AmitDiwan

AmitDiwan

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

1K+ 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 −{ ... Read More

Advertisements