Found 6705 Articles for Database

MongoDB aggregation and projection?

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" ...    }, ..   . "countryName" : "US", ... ...    "details" : [ ...       { ...          "Name" : "Robert", ...          "DueDate" : "2020-04-10" ... ...       }, ... ...       { ...     ... Read More

Query an array in MongoDB to fetch a specific value

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

649 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": { ...                "FullName": "Chris Brown" ...             } ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb034d15637cd592b2a4af1") } > db.demo761.insertOne( ...    { ...       ... Read More

How to update many documents with one query in MongoDB?

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, Value2:90}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb030a15637cd592b2a4af0") }Display all documents from a collection with the help of find() method −> db.demo760.find();This will produce the following output −{ "_id" : ObjectId("5eb0309f5637cd592b2a4aee"), "id" : 1, "details" : { "Value1" : 100, "Value2" : 50 } } { "_id" : ... Read More

MongoDB $regex operator i or I for case insensitive search

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" : ObjectId("5eb02baf5637cd592b2a4ae9") } > db.demo759.insertOne({SubjectName:"MONGODB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb02bb85637cd592b2a4aea") }Display all documents from a collection with the help of find() method −> db.demo759.find();This will produce the following output −{ "_id" : ObjectId("5eb02ba95637cd592b2a4ae7"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5eb02baa5637cd592b2a4ae8"), "SubjectName" : "MongoDB" } { "_id" ... Read More

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

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" : ObjectId("5eb025745637cd592b2a4ae2") } > db.demo757.insertOne( ...    { ...       "id": 102, ...       "Name": ["David"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb025755637cd592b2a4ae3") }Display all documents from a collection with the help of find() method −> db.demo757.find();This will produce the ... Read More

Check for duplicates in an array in MongoDB?

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 documents from a collection with the help of find() method −> db.demo756.find();This will produce the following output −{ "_id" : ObjectId("5eb01e0d5637cd592b2a4add"), "SubjectName" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5eb01e2b5637cd592b2a4ade"), "SubjectName" : [ "MongoDB", "MySQL", "MongoDB", "C", "C+", "MySQL" ] }Following is the query to check for ... Read More

Sort MongoDB field which contains both integer and decimal values?

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") } > db.demo755.insertOne({"Value":12.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e7fa930c785c834e575") } > db.demo755.insertOne({"Value":11.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e87a930c785c834e576") } > db.demo755.insertOne({"Value":6}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e97a930c785c834e577") }Display all documents from a collection with the help of find() method −> db.demo755.find();This will produce ... Read More

How to convert birth date records to age with MongoDB

AmitDiwan
Updated on 01-Jul-2020 06:41:02

2K+ Views

Let us create a collection with documents −> db.demo754.insertOne({"DateOfBirth":new Date("2000-05-03")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9b2da930c785c834e56f") } > db.demo754.insertOne({"DateOfBirth":new Date("2010-01-21")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9b34a930c785c834e570") } > db.demo754.insertOne({"DateOfBirth":new Date("2018-05-03")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9b3da930c785c834e571") }Display all documents from a collection with the help of find() method −> db.demo754.find();This will produce the following output −{ "_id" : ObjectId("5eae9b2da930c785c834e56f"), "DateOfBirth" : ISODate("2000-05-03T00:00:00Z") } { "_id" : ObjectId("5eae9b34a930c785c834e570"), "DateOfBirth" : ISODate("2010-01-21T00:00:00Z") } { "_id" : ObjectId("5eae9b3da930c785c834e571"), "DateOfBirth" : ISODate("2018-05-03T00:00:00Z") }Following is the query to convert date of birth to age −> db.demo754.aggregate( ... Read More

Filter specific values from a MongoDB document

AmitDiwan
Updated on 30-Jun-2020 08:15:02

575 Views

To filter specific values, use $filter in MongoDB. Let us create a collection with documents −> db.demo751.insertOne( ...    { ...       _id: 101, ...       details: [ ...          { Name: "Robert", id:110, Age:21}, ...          { Name: "Rae", id:110, Age:22}, ...          {Name: "Ralph", id:116, Age:23} ...       ] ...    } ... ); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo751.find().pretty();This will produce the following output −{    "_id" ... Read More

How to get max values for distinct elements in MongoDB

AmitDiwan
Updated on 30-Jun-2020 08:12:49

715 Views

To get max values for distinct elements, use $sort and $group in MongoDB aggregate(). Let us create a collection with documents −> db.demo750.insertOne({id:101, value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae74b2a930c785c834e566") } > db.demo750.insertOne({id:102, value:40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae74c8a930c785c834e567") } > db.demo750.insertOne({id:101, value:110}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae74dba930c785c834e568") }Display all documents from a collection with the help of find() method −> db.demo750.find();This will produce the following output −{ "_id" : ObjectId("5eae74b2a930c785c834e566"), "id" : 101, "value" : 50 } { "_id" : ObjectId("5eae74c8a930c785c834e567"), "id" : 102, "value" : 40 } ... Read More

Advertisements