2017-02-09 9 views
1

mongocxx 드라이버를 사용하여 특정 날짜 범위 내에있는 문서 (주식 데이터)를 mongodb에 쿼리해야합니다. mongodb : 날짜 범위를 통한 쿼리

{ 
    date : ISODate("2010-01-01T00:00:00Z"), 
    open : 12.00, 
    high : 13.00, 
    low : 11.00, 
    close : 12.50, 
    volume : 100000 
} 

내가 한 재고 당 수집 및 수집 당이 문서의 수백, 다른 날짜에 각을 말해봐 :

는 다음과 같은 문서 형식을 고려하십시오.

만약 문자열 (yyyy-mm-dd)이 같은 형식의 사용자 공급이 날짜 : 나는 "시작일"과 "종료일"사이의 날짜로 모든 파일을 얻을 몽고를 조회 할 수있는 방법

std::string start_date = "2010-01-01"; 
std::string end_date = "2010-02-05"; 

(포함한)?

참고 : 나는 MongoDB를 3.2.12을 사용하고, mongocxx 드라이버 버전 3.0.2

감사합니다,

답변

0

불행하게도, 임의의 시간대와 문자열에서 날짜를 구문 분석 할 수있는 방법이있을 것 같지 않습니다; 구문 분석 한 모든 날짜는 사용자의 로켈에있는 것으로 간주되므로 데이터베이스에 저장된 UTC 날짜를 올바르게 쿼리 할 수 ​​있도록 오프셋을 제공해야합니다. 이상적으로 이것은 사용자가 문자열을 제공 할 때 생성 될 수 있지만 이것은 분명히 응용 프로그램의 성격에 달려 있습니다.

오프셋 및 날짜 문자열을 얻은 후에는 std::get_time이 가장 좋습니다. 그런 다음 std::tmbsoncxx::types::b_date을 구성하고 평소와 같이 쿼리 할 수있는 형식으로 변환하면됩니다. 다음은 작업 수행 일부 샘플 코드입니다 : 나는이 질문을 게시 이후의 시간에

#include <chrono> 
#include <cstdint> 
#include <ctime> 
#include <iomanip> 
#include <iostream> 
#include <ostream> 
#include <sstream> 
#include <string> 

#include <bsoncxx/builder/basic/document.hpp> 
#include <bsoncxx/builder/basic/kvp.hpp> 
#include <bsoncxx/builder/basic/sub_document.hpp> 
#include <bsoncxx/json.hpp> 
#include <bsoncxx/types.hpp> 
#include <mongocxx/client.hpp> 
#include <mongocxx/uri.hpp> 

bsoncxx::types::b_date read_date(const std::string& date, 
           std::int32_t offset_from_utc) { 
    std::tm utc_tm{}; 
    std::istringstream ss{date}; 

    // Read time into std::tm. 
    ss >> std::get_time(&utc_tm, "%Y-%m-%d"); 

    // Convert std::tm to std::time_t. 
    std::time_t utc_time = std::mktime(&utc_tm); 

    // Convert std::time_t std::chrono::systemclock::time_point. 
    std::chrono::system_clock::time_point time_point = 
     std::chrono::system_clock::from_time_t(utc_time); 

    return bsoncxx::types::b_date{time_point + 
            std::chrono::hours{offset_from_utc}}; 
} 

int main() { 
    // User inputs 

    std::string start_date = "2010-01-01"; 
    std::string end_date = "2010-02-05"; 

    std::int32_t offset_from_utc = -5; 

    // Code to execute query 

    mongocxx::client client{mongocxx::uri{}}; 
    mongocxx::collection coll = client["db_name"]["coll_name"]; 

    bsoncxx::builder::basic::document filter; 
    filter.append(bsoncxx::builder::basic::kvp(
     "date", [start_date, end_date, 
       offset_from_utc](bsoncxx::builder::basic::sub_document sd) { 
      sd.append(bsoncxx::builder::basic::kvp(
       "$gte", read_date(start_date, offset_from_utc))); 
      sd.append(bsoncxx::builder::basic::kvp(
       "$lte", read_date(end_date, offset_from_utc))); 
     })); 

    for (auto&& result : coll.find(filter.view())) { 
     std::cout << bsoncxx::to_json(result) << std::endl; 
    } 
} 
+0

, 나는 [이] (발견 http://stackoverflow.com/questions/21021388/how-to-parse-a- 첫 번째 부분을 도왔던 c11-stdchrono-time-point-or-similar) 포스트에 대한 날짜 문자열. 그러나 UTC 시간을 고려하지 않았기 때문에 쿼리가 작동하지 않아서이를 지적 해 주셔서 감사합니다! – tmalt

관련 문제