簡體   English   中英

將 MongoDB 查詢更改為 Javascript 中的 Postgres 查詢

[英]Changing a MongoDB query into a Postgres query in Javascript

我在轉換此查詢時遇到了挑戰。 我在其他人身上取得了成功,但這個領域結合了多個領域,我需要一些幫助。 這是為了獲取創建預訂的日期。

//convert this:

var getByDate = (restId, date) => {
  return new Promise((resolve, reject) => {
    //  create search-between dates for the date sought
    var workDate = new Date(date);
    var fromDate = new Date(workDate.getFullYear(), workDate.getMonth(), workDate.getDate());
    var thruDate = new Date(fromDate);
    thruDate.setDate(thruDate.getDate() + 1);
    Reservation.find({
        restaurant_id: restId
      })
      .where('reservation_time').gte(fromDate).lt(thruDate)
      .exec((err, reservations) => {
        resolve(reservations);
      });
  });
};

//convert into PostgreSql query:

var getByDate = function (restId, date, callback) {
  return new Promise((resolve, reject) => {
    //  create search-between dates for the date sought
    var workDate = new Date(date);
    var fromDate = new Date(workDate.getFullYear(), workDate.getMonth(), workDate.getDate());
    var thruDate = new Date(fromDate);
    thruDate.setDate(thruDate.getDate() + 1);
    client.query('SELECT * FROM reservationSchema WHERE restaurant_id = restId ;', (error, results) => { //! NEED HELP ON THIS LINE
      if (error) {
        throw error;
      }
      callback(results.rows);
    })
      // .where('reservation_time').gte(fromDate).lt(thruDate) //! old section commented out
      // .exec((err, reservations) => {
      //   resolve(reservations);
      // });
  });
};

我想出了這個:

var getByDate = function (restId, date, callback) {
  return new Promise((resolve, reject) => {
    //  create search-between dates for the date sought
    var workDate = new Date(date);
    var fromDate = new Date(workDate.getFullYear(), workDate.getMonth(), workDate.getDate());
    var thruDate = new Date(fromDate);
    thruDate.setDate(thruDate.getDate() + 1);
    client.query('SELECT * FROM reservationSchema WHERE restaurant_id = $1 and reservation_time >= $2, and reservation_time < $3;', [restId, fromDate, thruDate], (error, results) => {
      if (error) {
        throw error;
      }
      callback(results.rows);
    })
  });
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM