Convert string to number mongodb

Docs HomeMongoDB Manual

$toInt

Converts a value to an integer. If the value cannot be converted to an integer, $toInt errors. If the value is null or missing, $toInt returns null.

$toInt has the following syntax:

The $toInt takes any valid expression.

The $toInt is a shorthand for the following $convert expression:

{ $convert: { input: , to: "int" } }

Tip

See also:

The following table lists the input types that can be converted to an integer:

Input Type

Behavior

Boolean

Returns 0 for false.

Returns 1 for true.

Double

Returns truncated value.

The truncated double value must fall within the minimum and maximum value for an integer.

You cannot convert a double value whose truncated value is less than the minimum integer value or is greater than the maximum integer value.

Decimal

Returns truncated value.

The truncated decimal value must fall within the minimum and maximum value for an integer.

You cannot convert a decimal value whose truncated value is less than the minimum integer value or is greater than the maximum integer value.

Integer

No-op. Returns the integer value.

Long

Returns the long value as an integer.

The long value must fall within the minimum and maximum value for an integer.

You cannot convert a long value that is less than the minimum integer value or is greater than the maximum integer value.

String

Returns the numerical value of the string as an integer.

The string value must be a base 10 integer; e.g. "-5", "123456"].

You cannot convert a string value of a float or decimal or non-base 10 number [e.g. "-5.0", "0x6400"]

The following table lists some conversion to integer examples:

Example

Results

$toInt: true

1

$toInt: false

0

$toInt: 1.99999

1

$toInt: NumberDecimal["5.5000"]

5

$toInt: NumberDecimal["9223372036000.000"]

Error

$toInt: NumberLong["5000"]

5000

$toInt: NumberLong["922337203600"]

Error

$toInt: "-2"

-2

$toInt: "2.5"

Error

$toInt: null

null

Create a collection orders with the following documents:

db.orders.insertMany[ [
{ _id: 1, item: "apple", qty: "5", price: 10 },
{ _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] },
{ _id: 3, item: "ice cream", qty: "2", price: "4.99" },
{ _id: 4, item: "almonds" , qty: "5", price: 5 }
] ]

The following aggregation operation:

  • converts qty to an integer,

  • converts price to a decimal,

  • calculates the total price:

// Define stage to add convertedPrice and convertedQty fields with the converted price and qty values
priceQtyConversionStage = {
$addFields: {
convertedPrice: { $toDecimal: "$price" },
convertedQty: { $toInt: "$qty" },
}
};
// Define stage to calculate total price by multiplying convertedPrice and convertedQty fields
totalPriceCalculationStage = {
$project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }
};
db.orders.aggregate[ [
priceQtyConversionStage,
totalPriceCalculationStage
] ]

The operation returns the following documents:

{ _id: 1, item: 'apple', totalPrice: Decimal128["50"] },
{ _id: 2, item: 'pie', totalPrice: Decimal128["200.0"] },
{ _id: 3, item: 'ice cream', totalPrice: Decimal128["9.98"] },
{ _id: 4, item: 'almonds', totalPrice: Decimal128["25"] }

Note

If the conversion operation encounters an error, the aggregation operation stops and throws an error. To override this behavior, use $convert instead.

How do I change the datatype of a column in MongoDB?

You can change the data type of a field by using the data type selectors on the right of the field in the Atlas Cloud Cluster as well as MongoDB Compass . If you want to update it using Mongo shell or any specific drivers of MongoDB then you can refer to the $convert operator.

What is $Set in MongoDB?

$set outputs documents that contain all existing fields from the input documents and newly added fields. The $set stage is an alias for $addFields. Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

What is $project in MongoDB?

Definition. $project. Passes along the documents with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.

What are the data types in MongoDB?

The following are some of the most often used data types in MongoDB..
String. One of the most basic and widely used data types is the string. ... .
Integer. Numeric values are stored using the integer data type. ... .
Double. ... .
Boolean. ... .
Array. ... .
Object. ... .
Date. ... .
Timestamp..

Chủ Đề