Display Unique Records from MongoDB and Ignore Duplicates

AmitDiwan
Updated on 15-May-2020 08:11:33

397 Views

To display only unique records, use distinct() in MongoDB. Let us create a collection with documents −> db.demo613.insertOne({"Name":"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988bd4f6b89257f5584d88") } > db.demo613.insertOne({"Name":"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988bdbf6b89257f5584d89") } > db.demo613.insertOne({"Name":"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988bddf6b89257f5584d8a") } > db.demo613.insertOne({"Name":"David"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988be0f6b89257f5584d8b") } > db.demo613.insertOne({"Name":"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988be2f6b89257f5584d8c") } > db.demo613.insertOne({"Name":"Sam"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988be8f6b89257f5584d8d") }Display all documents from a collection with the help of find() method −> db.demo613.find();This will produce the following output −{ "_id" : ObjectId("5e988bd4f6b89257f5584d88"), "Name" : ... Read More

Select MongoDB Document That Does Not Contain a Specific Field

AmitDiwan
Updated on 15-May-2020 08:09:00

276 Views

Check for a specific field using MongoDB $exists. If that field does not exist in a document, then you need to display the same document with find().Let us create a collection with documents −> db.demo612.insertOne({id:1, "Info":[{Name:"Chris", Age:21}, {Name:"David"}]});{    "acknowledged" : true, "insertedId" : ObjectId("5e987372f6b89257f5584d87") }Display all documents from a collection with the help of find() method −> db.demo612.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e987372f6b89257f5584d87"),    "id" : 1,    "Info" : [       {          "Name" : "Chris",          "Age" : 21       },   ... Read More

Sort Documents in MongoDB 4 and Display a Single Field

AmitDiwan
Updated on 15-May-2020 08:07:27

154 Views

To sort documents in MongoDB 4, use sort(). To show only a single field, which is sorted, set it to 1.Let us create a collection with documents −> db.demo611.insertOne({"Name":"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e987110f6b89257f5584d83") } > db.demo611.insertOne({"Name":"Adam"});{    "acknowledged" : true, "insertedId" : ObjectId("5e987115f6b89257f5584d84") } > db.demo611.insertOne({"Name":"John"});{    "acknowledged" : true, "insertedId" : ObjectId("5e987118f6b89257f5584d85") } > db.demo611.insertOne({"Name":"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e98711bf6b89257f5584d86") }Display all documents from a collection with the help of find() method −> db.demo611.find(); This will produce the following output: { "_id" : ObjectId("5e987110f6b89257f5584d83"), "Name" : "Chris" } { "_id" : ObjectId("5e987115f6b89257f5584d84"), "Name" : ... Read More

Add Together Subset of Elements in MongoDB Aggregation

AmitDiwan
Updated on 15-May-2020 08:05:57

228 Views

To add together a subset of elements of an array, use $first along with $sum. Let us create a collection with documents −> db.demo610.insertOne({Values:[10, 20, 30, 40, 50]});{    "acknowledged" : true, "insertedId" : ObjectId("5e9747b8f57d0dc0b182d62e") }Display all documents from a collection with the help of find() method −> db.demo610.find().pretty()This will produce the following output −{    "_id" : ObjectId("5e9747b8f57d0dc0b182d62e"),    "Values" : [       10,       20,       30,       40,       50    ] }Here is the query to add together a subset of elements of an array in ... Read More

Remove Entire Array from Collection in MongoDB

AmitDiwan
Updated on 15-May-2020 08:04:29

471 Views

To remove the entire array from the collection, use $unset in MongoDB. Let us create a collection with documents −> db.demo609.insertOne({"ListOfSubject":["MySQL", "MongoDB"]});{    "acknowledged" : true, "insertedId" : ObjectId("5e974695f57d0dc0b182d62c") } > db.demo609.insertOne({"ListOfSubject":["Java"]});{    "acknowledged" : true, "insertedId" : ObjectId("5e97469af57d0dc0b182d62d") }Display all documents from a collection with the help of find() method −> db.demo609.find();This will produce the following output −{ "_id" : ObjectId("5e974695f57d0dc0b182d62c"), "ListOfSubject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5e97469af57d0dc0b182d62d"), "ListOfSubject" : [ "Java" ] }Here is the query to remove the entire array from collection −> db.demo609.update({}, {$unset:{"ListOfSubject":""}}, {multi:true}); WriteResult({ "nMatched" : 2, "nUpserted" : 0, ... Read More

Get Unique Values Within Two Arrays in One MongoDB Document

AmitDiwan
Updated on 15-May-2020 08:03:10

2K+ Views

To get unique values within two arrays in a document, use a $setUnion in aggregate(). The $setUnion takes two or more arrays and returns an array containing the elements that appear in any input array.Let us create a collection with documents −>db.demo608.insertOne({"ListOfName1":["John", "Chris", "Bob", "David"], "ListOfName2":["Bob", "Sam", "John", "Robert", "Chris"]} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e974542f57d0dc0b182d62b") }Display all documents from a collection with the help of find() method −> db.demo608.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e974542f57d0dc0b182d62b"),    "ListOfName1" : [       "John",       "Chris",       ... Read More

MongoDB Query to Update the Nested Document

AmitDiwan
Updated on 15-May-2020 07:59:58

470 Views

To update the nested document, use update() and within that, use dot notation. Let us create a collection with documents −> db.demo607.insertOne( ...    { ...       id:1, ...       "Info1" : { ...          "Name" : "Chris", ...          "Age" : 21, ... ...          "Info2" : { ...             "SubjectName" : "MongoDB", ...             "Marks" : 89 ...          } ...       } ...    } ... ); {   ... Read More

Get Rating Average in MongoDB Based on Duplicate IDs

AmitDiwan
Updated on 15-May-2020 07:52:30

356 Views

For average in MongoDB, use the $avg. Let us create a collection with documents. Here, we have duplicate ids with rating for each −> db.demo606.insertOne({id:1, rating:5});{    "acknowledged" : true, "insertedId" : ObjectId("5e972dfbf57d0dc0b182d623") } > db.demo606.insertOne({id:1, rating:4});{    "acknowledged" : true, "insertedId" : ObjectId("5e972dfef57d0dc0b182d624") } > db.demo606.insertOne({id:2, rating:3});{    "acknowledged" : true, "insertedId" : ObjectId("5e972e09f57d0dc0b182d625") } > db.demo606.insertOne({id:1, rating:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e972e0ef57d0dc0b182d626") } > db.demo606.insertOne({id:2, rating:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e972e15f57d0dc0b182d627") } > db.demo606.insertOne({id:2, rating:3});{    "acknowledged" : true, "insertedId" : ObjectId("5e972e1bf57d0dc0b182d628") }Display all documents from a collection with the help of find() method ... Read More

Update Quantity in MongoDB Based on Two Conditions

AmitDiwan
Updated on 15-May-2020 07:50:36

207 Views

For this, use UPDATE() method and within that set the two conditions. Let us create a collection with documents −> db.demo605.insertOne( ...    { ...       _id:1, ...       "Information" : [ ...          { ...             "id" : "Product-1", ...             "Quantity" : 50, ...          }, ...          { ...             "id" : "Product-2", ...             "Quantity" : 100, ...          }, ... Read More

Remove Duplicates from MongoDB Collection

AmitDiwan
Updated on 15-May-2020 07:48:00

3K+ Views

For this, set “unique:true” i.e. the unique constraint and avoid inserting duplicates as in the below syntax −db.yourCollectionName.ensureIndex({yourFieldName: 1}, {unique: true, dropDups: true})To understand the above syntax, let us create a collection with documents. Here, duplicate insertion won’t be allowed −> db.demo604.ensureIndex({FirstName: 1}, {unique: true, dropDups: true});{    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo604.insertOne({FirstName:"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e960887ed011c280a0905d8") } > db.demo604.insertOne({FirstName:"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e96088aed011c280a0905d9") } > db.demo604.insertOne({FirstName:"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e96088ded011c280a0905da") } > db.demo604.insertOne({FirstName:"Chris"}); 2020-04-15T00:31:35.978+0530 ... Read More

Advertisements