カンマを含む文字列をdoubleに変換するMongoDBクエリ
このような変換には、aggregate()を使用します。ドキュメントを使用してコレクションを作成しましょう-
> db.demo335.insertOne({"Value":"45,67,78.0"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e522a1cf8647eb59e562091")
}
> db.demo335.insertOne({"Value":"17664,76,534.0"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e522a26f8647eb59e562092")
}
> db.demo335.insertOne({"Value":"8899,322,135,875.50"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e522a34f8647eb59e562093")
}
> db.demo335.insertOne({"Value":"1,533.07"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e522ab9f8647eb59e562094")
} find()メソッドを使用してコレクションからすべてのドキュメントを表示する-
> db.demo335.find();
これにより、次の出力が生成されます-
{ "_id" : ObjectId("5e522a1cf8647eb59e562091"), "Value" : "45,67,78.0" }
{ "_id" : ObjectId("5e522a26f8647eb59e562092"), "Value" : "17664,76,534.0" }
{ "_id" : ObjectId("5e522a34f8647eb59e562093"), "Value" : "8899,322,135,875.50" }
{ "_id" : ObjectId("5e522ab9f8647eb59e562094"), "Value" : "1,533.07" } 以下は、コンマを含む文字列をdoubleに変換するためのクエリです-
db.demo335.aggregate([
... { $project: {
... data: {
... $convert: {
... input: {
... $reduce: {
... input: {
... $split: ['$Value', ',']
... },
... initialValue: '',
... in: {
... $concat: ['$$value', '$$this']
... }
... }
... },
... to: 'double',
... onError: 0
... }
... }
... }}
... ]) これにより、次の出力が生成されます-
{ "_id" : ObjectId("5e522a1cf8647eb59e562091"), "data" : 456778 }
{ "_id" : ObjectId("5e522a26f8647eb59e562092"), "data" : 1766476534 }
{ "_id" : ObjectId("5e522a34f8647eb59e562093"), "data" : 8899322135875.5 }
{ "_id" : ObjectId("5e522ab9f8647eb59e562094"), "data" : 1533.07 } -
大文字と小文字を区別しない検索を使用したMongoDBクエリ?
大文字と小文字を区別しない検索の場合は、find()メソッドで正規表現を使用します。以下は構文です- db.demo572.find({"yourFieldName" : { '$regex':/^yourValue$/i}}); 上記の構文を理解するために、ドキュメントを使用してコレクションを作成しましょう- > db.demo572.insertOne({"CountryName":"US"});{ "acknowledged" : true, "in
-
Javaで文字列をDoubleに変換する
以下が私たちの文字列だとしましょう- String str = "55.2"; ここで、JavaでparseDouble()を使用して上記の文字列をdoubleに変換します- double res = Double.parseDouble("23.6"); 例 以下は、Javaで文字列をDoubleに変換するプログラムです- public class Demo { public static void main(String args[]){ String str = "5