MongoDBでは、$convertを使用できます 値を指定されたタイプに変換する集約パイプライン演算子。
任意の有効な式をdouble、string、ObjectId、boolean、Date、integer、long、またはdecimalに変換できます。
すべてのタイプを他のタイプに変換できるわけではありません。一部のタイプは、使用可能なMongoDBタイプのサブセットからのみ変換できます。たとえば、日付を整数に変換することはできません。
オプションでonErrorを使用できます エラーが発生した場合に何を返すかを指定するパラメーター。オプションでonNullを使用できます 入力値がnullまたは欠落している場合に何を返すかを指定するパラメーター。
サンプルデータ
samplesというコレクションがあるとします。 次のドキュメントで:
{
"_id" : ObjectId("6011e471c8eb4369cf6ad9d5"),
"double" : 123.75,
"string" : "123",
"boolean" : true,
"date" : ISODate("2020-12-31T23:30:15.123Z"),
"integer" : 123,
"long" : NumberLong(123),
"decimal" : NumberDecimal("123.75"),
"datestring" : "2021-02-15 06:53:55"
} 次の例は、各フィールドを他のタイプに変換する方法を示しています。
ObjectIdを文字列に変換
_id 上記のドキュメントのフィールドはObjectIdです。 ObjectIdを文字列に変換する例を次に示します。
db.samples.aggregate(
[
{
$project:
{
result:
{
$convert: {
input: "$_id",
to: "string",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
).pretty() 結果:
{
"_id" : ObjectId("6011e471c8eb4369cf6ad9d5"),
"result" : "6011e471c8eb4369cf6ad9d5"
} その結果、ObjectIdからの16進文字列が文字列として返されます。
Doubleを整数に変換
doubleを整数に変換すると、切り捨てられた値が返されます。
db.samples.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$double",
to: "int",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
) 結果:
{ "result" : 123 } 切り捨てられたdouble値は、整数の最小値と最大値の範囲内にある必要があります。そうでない場合はエラーが発生します。
また、切り捨てられた値が最小整数値よりも小さいか、最大整数値よりも大きいdouble値を変換することはできません。
文字列を整数に変換
文字列を整数に変換する場合、$convert 文字列の数値を整数として返します。
db.samples.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$string",
to: "int",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
) 結果:
{ "result" : 123 }
文字列値はbase10である必要があります 整数(例:"-123" 、"123" )そして整数の最小値と最大値の範囲内にあります。
ブール値を整数に変換
ブール値を整数に変換する場合、$convert 1を返します trueのブール値の場合 、および0 falseのブール値の場合 。
db.samples.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$boolean",
to: "int",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
) 結果:
{ "result" : 1 } 日付を文字列に変換
$convertを使用できます 日付を文字列として返します。
db.samples.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$date",
to: "string",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
) 結果:
{ "result" : "2020-12-31T23:30:15.123Z" } Dateオブジェクトは文字列に変換されました。
DoubleをDateに変換
次のタイプを日付に変換できます:
- ダブル
- 10進数
- 長い
- 文字列
- ObjectId
ダブルを日付に変換する例は次のとおりです。
db.samples.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$double",
to: "date",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
) 結果:
{ "result" : ISODate("1970-01-01T00:00:00.123Z") } 数値を日付に変換すると、数値は1970年1月1日からのミリ秒数を表します。
この例では、123のdouble値を指定しました 、1970年1月1日から123ミリ秒と解釈されました。
整数を10進数に変換
整数を10進数に変換する例を次に示します。
db.samples.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$integer",
to: "decimal",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
) 結果:
{ "result" : NumberDecimal("123.000000000000") } 文字列を日付に変換
日付/時刻文字列をDateオブジェクトに変換する例を次に示します。
db.samples.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$datestring",
to: "date",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
) 結果:
{ "result" : ISODate("2021-02-15T06:53:55Z") } 文字列をDateオブジェクトに変換する場合、文字列は次のような有効な日付文字列である必要があります。
- 2021-02-15
- 2021-02-15T06:53:55
- 2021-02-15T06:53:55Z
ブール値に変換
値をブール値に変換すると、結果はtrueになります。 またはfalse 、入力値によって異なります。
通常、数値の場合、これはfalseを返します。 値がゼロの場合(0 )、およびtrue その他の値の場合。
string、ObjectId、およびDateの値の場合、常にtrueを返します。 。
db.samples.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$string",
to: "bool",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
) 結果:
{ "result" : true } onError パラメータ
onErrorを使用できます エラーが発生した場合に指定された値を返すパラメータ。
dogsというコレクションがあるとします。 次のドキュメントで:
{ "_id" : 1, "name" : "Wag", "born" : "March 2020", "weight" : null }
以下は、エラーが原因で失敗する変換を実行しようとする例です。最初の例では、しない onErrorを使用する 。
db.dogs.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$born",
to: "int"
}
}
}
}
]
) 結果:
Error: command failed: {
"ok" : 0,
"errmsg" : "Failed to parse number 'March 2020' in $convert with no onError value: Did not consume whole string.",
"code" : 241,
"codeName" : "ConversionFailure"
} : aggregate failed :
example@sqldat.com/mongo/shell/utils.js:25:13
example@sqldat.com/mongo/shell/assert.js:18:14
example@sqldat.com/mongo/shell/assert.js:618:17
example@sqldat.com/mongo/shell/assert.js:708:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1046:12
@(shell):1:1 その結果、厄介なエラーメッセージが表示されました。
次の例は、onErrorを使用してこれを改善する方法を示しています。 パラメータ。
db.dogs.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$born",
to: "int",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
) 結果:
{ "result" : "An error occurred" } それでもドキュメントが返され、カスタムエラーメッセージがフィールドに表示されることに注意してください。
onNull パラメータ
オプションでonNullを使用できます 入力値がnullまたは欠落している場合に何を返すかを指定するパラメーター。
前のドキュメントを使用して、onNullをテストできます このようなパラメータ:
db.dogs.aggregate(
[
{
$project:
{
_id: 0,
result:
{
$convert: {
input: "$weight",
to: "decimal",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
) 結果:
{ "result" : "Input was null or empty" }