AmitDiwan has Published 10744 Articles

How can I sort documents in MongoDB 4 and display only a single field?

AmitDiwan

AmitDiwan

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

151 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"});{   ... Read More

How to add together a subset of elements of an array in MongoDB aggregation?

AmitDiwan

AmitDiwan

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

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

MongoDB query to remove entire array from collection?

AmitDiwan

AmitDiwan

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

466 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() ... Read More

Getting unique values within two arrays in one MongoDB document

AmitDiwan

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

MongoDB query to update the nested document?

AmitDiwan

AmitDiwan

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

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

How to get a rating average in MongoDB based on duplicate ids?

AmitDiwan

AmitDiwan

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

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

Update quantity in MongoDB based on two conditions?

AmitDiwan

AmitDiwan

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

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

How to remove duplicates from MongoDB Collection?

AmitDiwan

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

Difference between NumberLong(x) and NumberLong(“x”) in MongoDB?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 07:45:55

313 Views

The NumberLong(x) goes beyond its limit value and round off the value whereas NumberLong(“x”) does not.Now, we will consider a number and would use it for both NumberLong(x) and NumberLong(“x”) to see the difference.Let us create a collection with documents −> db.demo603.insert({"longValue" : NumberLong(988998985857575789)}); WriteResult({ "nInserted" : 1 }) > ... Read More

MongoDB aggregation of elements with similar ids in different documents?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 07:44:54

672 Views

For such grouping of documents, use $group in MongoDB aggregate(). Let us create a collection with documents −> db.demo602.insertOne({id:1, Name:"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e960080ed011c280a0905c9") } > db.demo602.insertOne({id:2, Name:"David"});{    "acknowledged" : true, "insertedId" : ObjectId("5e960086ed011c280a0905ca") } > db.demo602.insertOne({id:1, Name:"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e96008ced011c280a0905cb") } ... Read More

Advertisements