Skip to content

MongoDB Change the Type of a Field AQSA Spark By {Examples}

  • by

How to change the type of a field in MongoDB? In this article, we will explore different ways to change the type of field in MongoDB with examples. We can modify the type of fields in the documents without recreating the entire collection.

Let’s consider the following collection student with documents where we can see the types of fields needed to be changed.

#Create Collection
db.student.insertMany([
{
name: “Neutron”,
age: “20”,
marks: 98,
addmission: “2020-09-15”,
fees: “5000”,
projectCompleted: “true”

},
{
name: “Peter”,
age: “22”,
marks: 91,
addmission: “2023-02-09”,
fees: “6000”,
projectCompleted: “true”
}
]
)

1. Using $toInt Aggregation stage

You can use the MongoDB aggregation() function along with $toInt operator to convert a string type to an integer field. This operator parses the string and converts it to an integer.

Consider the following query of the $toInt operator. Here, we have used the aggregation pipeline which is called the $addFields operator which adds a new field fees to each document in the student collection. Then, the fees value is changed from a string type to an integer type by using the $toInt operator.

#Usage of $toInt Aggregation stage
db.student.aggregate([
{
$addFields: {
fees: { $toInt: “$fees” }
}
}
])

The output shows the documents with the fees field that contains now integer values.

2. Using $toDate Aggregation stage

As in the aforementioned example, we have performed the conversion from a string to an integer.

Now, we use the $dateToString aggregation operator to convert the date value to a string value. The field admission is stored with the date value in all the documents of the collection student.

Now, consider the following query where the aggregation pipeline uses the $project operator first to reshape documents by adding a new field addmissionDate. Then, we have a $toDate operator which transforms the admission field from a string data type to a date data type.

#Usage of $toDate Aggregation stage
db.student.aggregate([
{
$project: {
addmissionDate: {
$toDate: “$addmission”
}
}
}
])

The output here displayed the new field admissionDate with the date values in an ISODate format.

3. Using $toString Aggregation stage

Next is the $toString aggregation operator that can be used to convert a non-string field to a string field. A string value can be created using this operator by converting an integer, date, or Boolean value.

For example, the marks field of all the documents in the collection student contains the integer type values. We need to perform the conversion from integer to string type on this marks field. In this case, the $project stage adds a new field marksString. Then, we called the $toString operator within the marksString which converts the marks field from a numeric data type to a string data type for all documents in the student collection.

#Usage of $toString Aggregation stage
db.student.aggregate([
{
$project: {
marksString: {
$toString: “$marks”
}
}
}
])

Hence, the output yielded below displays the marksString field having the string type values.

4. Using $toConvert Aggregation stage

Finally, we have the $convert aggregation operator for converting the data type of a field in MongoDB. This operator is very effective and can convert a field to a different data type, or it can convert a field to null if the conversion is not possible.

For example, we have a field age in the documents which are of string type. Now, we use the $toConvert operator to change it into the integer type.

Consider the following query where we have first added the new field age that is converted to an integer data type. After that, with the help of the $toConvert operator, we change the age field from a string data type to an integer data type. The input parameter specifies the source field to convert, and the to parameter specifies the target data type. Lastly, we have a $out operator which overwrites the existing collection student with the new one.

#Usage of $toConvert Aggregation stage
db.student.aggregate([
{
$project: {
_id: 1,
age: {
$convert: {
input: “$age”,
to: “int”
}
}
}
},
{
$out: “student”
}
])

The output is yielded where the age field is now changed into the integer type.

5. Conclusion

In conclusion, with the right method, we can easily change the type of field to suit our needs. The examples we have explored in this article demonstrate some of MongoDB’s most common scenarios for changing field types.

 How to change the type of a field in MongoDB? In this article, we will explore different ways to change the type of field in MongoDB with examples. We can modify the type of fields in the documents without recreating the entire collection. Let’s consider the following collection student with documents where we can see  Read More MongoDB 

Leave a Reply

Your email address will not be published. Required fields are marked *