Found 1659 Articles for Big Data Analytics

Get MongoDB documents with max attribute per group in a collection?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

319 Views

You can get documents with max attribute per group in a collection using $sort operator along with $group statement.To understand the concept further, let us create a collection with document. The query to create a collection with document is as follows −> db.maxAttributePerGroup.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith    ", "StudentAge":29, "StudentId":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76ee341e9c5dd6f1f78277") } > db.maxAttributePerGroup.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylo    r", "StudentAge":19, "StudentId":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76ee4e1e9c5dd6f1f78278") } > db.maxAttributePerGroup.insertOne({"StudentFirstName":"Adam", "StudentLastName":"Smit    h", "StudentAge":34, "StudentId":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76ee631e9c5dd6f1f78279") } > db.maxAttributePerGroup.insertOne({"StudentFirstName":"Bob", "StudentLastName":"Taylor"    , "StudentAge":58, "StudentId":20}); { ... Read More

How to replace substring in MongoDB document?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

861 Views

In order to replace substring in MongoDB document, you can use the replace() function. To understand it further, let us create a collection with document. The query to create a collection with document is as follows −> db.replaceSubstringDemo.insertOne({"WebsiteURL":"www.gogle.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76eaf21e9c5dd6f1f78276") }Display all documents from a collection with the help of find() method. The query is as follows −> db.replaceSubstringDemo.find().pretty();Output{    "_id" : ObjectId("5c76eaf21e9c5dd6f1f78276"),    "WebsiteURL" : "www.gogle.com" }Here is the query to replace substring in MongoDB document −> db.replaceSubstringDemo.find({WebsiteURL:"www.gogle.com"}).forEach(function(url, k){    ... url.WebsiteURL=url.WebsiteURL.replace("www.gogle.com", "www.google.com");    ... db.replaceSubstringDemo.save(url)    ... });Let us display the ... Read More

Get a count of total documents with MongoDB while using limit?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

632 Views

You can use $facet operator for this. To understand the concept, let us create a collection with document. The query to create a collection with document is as follows −> db.totalDocumentDemo.insertOne({"InstructorId":100, "InstructorName":"Larry", "InstructorFav ouriteSubject":["Java", "MongoDB", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76e6701e9c5dd6f1f78274") } > db.totalDocumentDemo.insertOne({"InstructorId":200, "InstructorName":"Sam", "InstructorFav ouriteSubject":["SQL Server", "C#", "Javascript"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76e69c1e9c5dd6f1f78275") }Display all documents from a collection with the help of find() method. The query is as follows −> db.totalDocumentDemo.find().pretty();Output{    "_id" : ObjectId("5c76e6701e9c5dd6f1f78274"),    "InstructorId" : 100,    "InstructorName" : "Larry",    "InstructorFavouriteSubject" : [     ... Read More

MongoDB Regex Search on Integer Value?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

1K+ Views

To perform Regex search on integer value, you need to use $where operator. The syntax is as follows:db.yourCollectionName.find({ $where: "/^yourIntegerPatternValue.*/.test(this.yourFieldName)" });To understand the above concept, let us create a collection with document. The query to create a collection with document is as follows:> db.regExpOnIntegerDemo.insertOne({"StudentId":2341234}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c70370c75eb1743ddddce21") } > db.regExpOnIntegerDemo.insertOne({"StudentId":123234}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c70371175eb1743ddddce22") } > db.regExpOnIntegerDemo.insertOne({"StudentId":9871234}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c70371875eb1743ddddce23") } > db.regExpOnIntegerDemo.insertOne({"StudentId":2345612}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c70372275eb1743ddddce24") } > db.regExpOnIntegerDemo.insertOne({"StudentId":1239812345}); {    "acknowledged" : true,    "insertedId" : ... Read More

How to sort inner array in MongoDB?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

3K+ Views

You can achieve this with the help of aggregate framework in MongoDB. To understand it, let us create a collection with document. The query to create a collection with document is as follows:> db.sortInnerArrayDemo.insertOne( ... ...    { ...       "EmployeeDetails": ...       { ...          "EmployeeAddress": ...          { ...             "EmployeeCountry": ...             [ ...                { ...                   "EmployeeZipCode":1003, ...       ... Read More

Querying internal array size in MongoDB?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

160 Views

You can use the $size operator for internal array size in MongoDB. The syntax is as follows:db.internalArraySizeDemo.aggregate(    [       {          $group: {             _id:yourObjectIdValue,             anyFieldName: {$first: {$size: "$yourArrayName" }}          }       }    ] );To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents are as follows:>db.internalArraySizeDemo.insertOne({"EmployeeName":"Mike", "EmployeeTechnology":["Jav a Web Development", "Python Web Development"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6eff586fd07954a48906b2") } > ... Read More

Add new field to every document in a MongoDB collection?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

2K+ Views

To add new field to every document in a MongoDB collection, you can use $set operator. The syntax is as follows:db.yourCollectionName.update({}, { $set: {"yourFieldName": yourValue} }, false, true);To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows:>db.addNewFieldToEveryDocument.insertOne({"StudentName":"John", "StudentAddress":"US "}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6efc0b6fd07954a48906ae") } >db.addNewFieldToEveryDocument.insertOne({"StudentName":"David", "StudentAddress":"U K"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6efc0b6fd07954a48906af") } >db.addNewFieldToEveryDocument.insertOne({"StudentName":"Carol", "StudentAddress":"U K"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6efc0b6fd07954a48906b0") } >db.addNewFieldToEveryDocument.insertOne({"StudentName":"Bob", "StudentAddress":"US" }); {    "acknowledged" : true,    "insertedId" : ... Read More

How to remove a field completely from a MongoDB document?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

799 Views

You can use $unset operator to remove a field completely from a MongoDb document. The syntax is as follows:db.yourCollectionName.update({}, {$unset: {yourFieldName:1}}, false, true);To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents are as follows:> db.removeFieldCompletlyDemo.insertOne({"StudentName":"Larry", "StudentFavouriteSubject": ["Java", "C", "C++", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ef55a6fd07954a48906a3") } > db.removeFieldCompletlyDemo.insertOne({"StudentName":"Mike", "StudentFavouriteSubject": ["Javascript", "HTML5", "CSS3"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ef57b6fd07954a48906a4") } > db.removeFieldCompletlyDemo.insertOne({"StudentName":"Sam", "StudentFavouriteSubject": ["MongoDB", "MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ef59c6fd07954a48906a5") }Display all documents from a collection ... Read More

Converting string to date in MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25

884 Views

To convert the string to date in MongoDB, use the following syntax:db.yourCollectionName.aggregate(    [       {          $project:          {             anyVariableName:             {                $dateFromString:                {                   dateString: '$yourFieldName’                }             }          }       }    ] );To understand the above syntax, ... Read More

Group by dates in MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25

497 Views

You can use aggregate framework to group by dates in MongoDB. Let us first create a collection with some documents. The query to create a collection with documents are as follows:> db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate()}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ee4df6fd07954a4890695") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2019-01-31 15:20:09.234Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ee51c6fd07954a4890696") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2017-04-21 16:12:13.240Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ee5336fd07954a4890697") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2016-05-25 19:11:21.130Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ee54b6fd07954a4890698") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2016-05-25 19:11:21.130Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ee8de6fd07954a4890699") } > ... Read More

Advertisements