将方法转换为mongo查询

时间:2019-12-23 17:48:21

标签: node.js mongodb mongoose

有人知道我如何将Javascript中的以下逻辑转换为mongodb查询吗?

exports.payment = async function(req, res) {
  let _id = mongoose.Types.ObjectId(req.params.id);
  let query = { _id: _id };
  let finalPrice = 0;
  const timeSpentInMinutes = (rental.end.date - rental.start.date) / 60000;
  const timeSpentInHours = (rental.end.date - rental.start.date) / 3600000;

  if (rental.rentalMethod == 'minutes') {
    finalPrice = 1 + timeSpentInMinutes * 0.15;
  }

  if (rental.rentalMethod == 'pack') {
    if (timeSpentInHours > 0 && timeSpentInHours <= 1) finalPrice = 6;
    else if (timeSpentInHours > 1 && timeSpentInHours <= 2) finalPrice = 10;
    else finalPrice = 25;
  }
  console.log('finalPrice' + finalPrice);

  Rental.findOneAndUpdate(
    query,
    { $set: { price: finalPrice } },
    { upsert: true },
    function(err, doc) {
      if (err) return res.send({ error: err });

      return res.send('Succesfully saved.' + doc);
    }
  );
};

这是我正在尝试构建的控制器

这是示例数据

{
    "_id" : ObjectId("5e00726799c60c223f90bb61"),
    "start" : {
        "date" : ISODate("2019-12-23T07:53:11.719Z")
    },
    "rentalMethod" : "minutes",
    "price" : 10,
    "vehicle" : ObjectId("5df8bd4d79ca3b3ecd23bf42"),
    "__v" : 0,
    "end" : {
        "date" : ISODate("2019-12-23T17:18:15.424Z")
    }
}

这是我的出租方式吗?

'use strict';
const mongoose = require('mongoose');
let Schema = mongoose.Schema;
/**
 * @typedef RentalSchema
 * @property {string} code.required
 */
let RentalSchema = new Schema({
  start: {
    date: {
      type: Date
      /*       default: Date.now
       */
    },
    location: {
      index: {
        type: String
      },
      type: {
        type: String,
        enum: ['Point']
        /*         required: true
         */
      },
      coordinates: {
        type: []
        /*         required: true
         */
      }
    },
    range: { type: Number }
  },
  end: {
    date: {
      type: Date
      /*       default: Date.now
       */
    },

    location: {
      index: {
        type: String
      },
      type: {
        type: String,
        enum: ['Point']
        /*         required: true
         */
      },
      coordinates: {
        type: []
        /*         required: true
         */
      }
    },
    range: { type: Number }
  },
  price: {
    type: Number,
    required: true
  },
  rentalMethod: {
    type: String,
    enum: ['minutes', 'pack'],
    default: 'minutes'
  },
  /* code: {
    type: Number,
    required: true
  }, */
  vehicle: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Vehicles'
  },
  place: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Places'
  },
  custo_aluguer: {
    type: Number
  }
});

RentalSchema.index({ location: '2dsphere' });
RentalSchema.index({ 'start.location': '2dsphere' });
RentalSchema.index({ 'end.location': '2dsphere' });

module.exports = mongoose.model('Rentals', RentalSchema, 'Rentals');

我不知道如何在Mongoose查询中租用出租汽车,我不知道如何在mongodb中构建查询。请问有人有技巧吗?

0 个答案:

没有答案
相关问题