Found 1718 Questions for Big Data Analytics

Add new field to every document in a MongoDB collection?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25
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
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
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
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

How to change the type of a field in MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25
Let us convert string type to int for an example. Aggregation does not allow us to directly change the type of a field; therefore, you need to write a code to convert the type of a field.At first, create a collection with document. After that we will get the type of every field. The query to create a collection with document is as follows>db.changeDataType.insertOne({"StudentName":"Larry", "StudentAge":23, "StudentZipCode":" 10001", "isProgrammer":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ed4976fd07954a4890694") }Display all documents from a collection with the help of find() method. The query is as follows:> db.changeDataType.find().pretty();The following is the output:{   ... Read More

How to get the last N records in MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25
To get the last N records in MongoDB, you need to use limit(). The syntax is as follows:db.yourCollectionName.find().sort({$natural:-1}).limit(yourValue);To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Maxwell"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ecf3d6fd07954a4890689") } > db.getLastNRecordsDemo.insertOne({"EmployeeName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ecf496fd07954a489068a") } > db.getLastNRecordsDemo.insertOne({"EmployeeName":"Bob"}); { "acknowledged" : true,    "insertedId" : ObjectId("5c6ecf4e6fd07954a489068b") } > db.getLastNRecordsDemo.insertOne({"EmployeeName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ecf546fd07954a489068c") } > db.getLastNRecordsDemo.insertOne({"EmployeeName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ecf596fd07954a489068d") } > db.getLastNRecordsDemo.insertOne({"EmployeeName":"Mike"}); ... Read More

How do you remove an array element by its index in MongoDB

Nancy Den
Updated on 30-Jul-2019 22:30:25
To remove array element by its index in MongoDB, you can use $unset and $pull operator. There are two steps to remove array elements from an array.The syntax for the same is as follows:db.yourCollectionName.update({}, {$unset:{"yourArrayListName.yourPosition":yourPositionValue}}; db.yourCollectionName.update({}, {$pull:{"yourArrayListName":null}});To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:>db.removeArrayElements.insertOne({"StudentName":"Larry", "StudentAge":23, "TechnicalSub ject":["C", "C++", "Java", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ea4879c4643706aef56d2") }Display all documents from a collection with the help of find() method. The query is as follows:> db.removeArrayElements.find().pretty();The following is the output:{    "_id" : ObjectId("5c6ea4879c4643706aef56d2"), ... Read More

Best way to store date/time in MongoDB?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25
There are two different ways by which you can store date/time in MongoDB. In the first approach, you can use Date objects like JavaScript. The Date object is the best way to store date/time in MongoDB. The syntax is as follows:new Date();In the second approach, you can use ISODate(). The syntax is as follows:new ISODate();To understand the above syntax, let us create a collection with documents following the first approach. The query to create a collection with document is as follows:The first approach:> db.ProductsInformation.insertOne({"ProductId":"Product-1", "ProductDeliveryDateTime":new Date()}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ec6786fd07954a4890686") }The second approach:> db.ProductsInformation.insertOne({"ProductId":"Product-2", "ProductDeliveryDateTime":new ... Read More

How to update the _id of a MongoDB Document?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25
You cannot update it but you can save a new id and remove the old id. Follow some steps in order to update the _id of a MongoDB. The steps are as follows:Step1: In the first step, you need to store ObjectId into a variable.anyVariableName=db.yourCollectionName.findOne({_id:yourObjectIdValue)});Step 2: In the second step, you need to set a new id.yourDeclaredVariableName._id=yourNewObjectIdValue;Step 3: In the third step, you need to insert new id on a document.db.yourCollectionName.insert(yourDeclaredVariableName);Step 4: In the fourth step, you need to remove the old id.db.yourCollectionName.remove({_id:yourOldObjectIdValue)});To understand the above steps, let us create a collection with document. The query to create a collection ... Read More

Count the number of items in an array in MongoDB?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25
To count the number of items in an array, you can use $size operator. The syntax is as follows:db.yourCollectionName.aggregate({$project:{anyFieldName:{$size:"$yourArrayName"}}}).prett y();To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:>db.getSizeOfArray.insertOne({"StudentId":1, "StudentName":"Larry", "StudentMarks":[87, 34, 5 6, 77, 89, 90]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ebc536fd07954a4890680") } >db.getSizeOfArray.insertOne({"StudentId":2, "StudentName":"Sam", "StudentMarks":[90, 76, 56 ]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ebc6b6fd07954a4890681") } >db.getSizeOfArray.insertOne({"StudentId":3, "StudentName":"Carol", "StudentMarks":[90, 76]}) ; {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ebc7a6fd07954a4890682") }Now you can display all documents from a collection with ... Read More
Advertisements