AmitDiwan has Published 10744 Articles

MongoDB Aggregate group multiple result?

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 07:03:26

743 Views

To aggregate multiple result, use $group in MongoDB. Let us create a collection with documents −> db.demo765.insertOne( ... ...    { ...       Name:"John", ...       "Category":"ComputerScience", ...       "SubjectName":"MongoDB", ...       "Marks":75 ...    } ... ); {    "acknowledged" : ... Read More

MongoDB checking for not null?

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 07:00:01

791 Views

Use $ne to check for not null. Let us create a collection with documents −> db.demo764.insertOne({"LoginUserName":"Chris", "LoginPassword":"Chris_12"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb04ee55637cd592b2a4afc") } > db.demo764.insertOne({"LoginUserName":"Chris", "LoginPassword":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb04eee5637cd592b2a4afd") } > db.demo764.insertOne({"LoginUserName":"Chris", "LoginPassword":""}); {    "acknowledged" : true,    "insertedId" ... Read More

Querying on an array of objects for specific nested documents with MongoDB?

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 06:58:37

1K+ Views

To query on an array of objects for nested documents, use find(). Let us create a collection with documents −> db.demo763.insertOne( ...    { ...       _id:1, ...       CountryName:"US", ...       "studentInformation": [ ...          { ...       ... Read More

MongoDB aggregation and projection?

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 06:57:21

1K+ Views

For this, use $project along with aggregate(). The $project in aggregation passes along the documents with the requested fields to the next stage in the pipeline.Let us create a collection with documents −> db.demo762.insertOne({ ...    "_id" : { ...       "userId":101, ...       "userName":"Chris" ... ... Read More

Query an array in MongoDB to fetch a specific value

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 06:54:10

650 Views

To fetch a specific value from an array, use aggregate() along with $project. Let us create a collection with documents −> db.demo761.insertOne( ...    { ...       "details": [ ...          { ...             "student": { ...       ... Read More

How to update many documents with one query in MongoDB?

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 06:52:18

583 Views

To update many documents with a single query, use bulkWrite() in MongoDB. Let us create a collection with documents −> db.demo760.insertOne({id:1, details:{Value1:100, Value2:50}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb0309f5637cd592b2a4aee") } > db.demo760.insertOne({id:2, details:{Value1:60, Value2:70}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb030a15637cd592b2a4aef") } > db.demo760.insertOne({id:3, details:{Value1:80, ... Read More

MongoDB $regex operator i or I for case insensitive search

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 06:49:04

567 Views

For this, you need to use case insensitive (i). Let us create a collection with documents −> db.demo759.insertOne({SubjectName:"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb02ba95637cd592b2a4ae7") } > db.demo759.insertOne({SubjectName:"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb02baa5637cd592b2a4ae8") } > db.demo759.insertOne({SubjectName:"mongodb"}); {    "acknowledged" : true,    "insertedId" : ... Read More

MongoDB Convert One record with an array to multiple records in a new collection?

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 06:47:20

508 Views

For this, you can use $out along with aggregate() and $unwind. Let us create a collection with documents −> db.demo757.insertOne( ...    { ...       "id": 101, ...       "Name": ["John", "Bob", "Chris"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ... Read More

Check for duplicates in an array in MongoDB?

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 06:46:00

1K+ Views

To check for duplicates in an array, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo756.insertOne({"SubjectName":["MySQL", "MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb01e0d5637cd592b2a4add") } > db.demo756.insertOne({"SubjectName":["MongoDB", "MySQL", "MongoDB", "C", "C+", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb01e2b5637cd592b2a4ade") }Display all ... Read More

Sort MongoDB field which contains both integer and decimal values?

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 06:42:25

386 Views

To sort, use sort() in MongoDB. Let us create a collection with documents −> db.demo755.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e72a930c785c834e572") } > db.demo755.insertOne({"Value":10.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e75a930c785c834e573") } > db.demo755.insertOne({"Value":9.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e79a930c785c834e574") } > ... Read More

Advertisements