私はMongoDBでjavaを使用してWebサービスに取り組んでおり、「2015-04-1616:32:49」という日時がフィールドstart_timeとend_timefromのフィールドにある日時の間にあるかどうかを確認しようとしています。 table.mysqlで次のクエリを試しました
select * from tablename where Employee_id = 101 and '2015-04-16 16:32:49' between start_time and end_time;
次のコードを使用して、MongoDBでJavaを使用して同じものを複製しようとしています
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("employeeID", "101");
String date = "2015-04-17 11:02:49";
whereQuery.put(date, new BasicDBObject("$gt", "start_time ")
.append("$lt", "end_time "));
DBCursor cursor = col.find(whereQuery);
しかし、コードは機能していません。テーブルの2つの列で指定された日付を比較していません。これを行うための正しい構文は何ですか。
このコードを試してみると
whereQuery.put(someOtherField, new BasicDBObject("$gt", "2015-04-17 11:02:49")
.append("$lt", "2015-04-19 11:02:49"));
それは機能しています私は日付文字列をテーブル全体の2つのフィールドと比較するための構文が必要です
クエリは大まかに次のように変換されます。
select * from tablename where Employee_id =101 and '2015-04-16 16:32:49' > "start_time" and '2015-04-16 16:32:49' < "end_time";
文字列"start_time"
と"end_time"
を比較しています'2015-04-16 16:32:49'
。
正しいクエリは次のようになります:-
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("employeeID", "101");
String date_string = "2015-04-17 11:02:49";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Date date = format.parse(date_string);
whereQuery.put("start_time", new BasicDBObject("$lt", date));
whereQuery.put("end_time", new BasicDBObject("$gt", date));
DBCursor cursor = col.find(whereQuery);
通常のJAVA日付オブジェクトを使用できます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加