0

Схема

code: "0"
comments: []
email: "[email protected]"
moneyAmount: 8000
orders: {
  followers: 100
  orderCreationDate: "2021-03-21T13:30:32.807Z"
  orderStatus: "play"
  tarifCost: 4
  taskId: "127"
  totalOrderValue: 1000
  userName: "rttt"
  _id: "60574a788b96cb2f3e359eab"
}
password: "$2b$10$MnilU5assKBScvHp7avMxux7qRefDa0b9phC1yT0Ce3LVyLoX0.B6"
paymentCards: []
status: "USER"
tikTokAccounts: []
tikTokVideos: []
userName: "[email protected]"
verified: true
__v: 22
      const users = await User.aggregate([
        { $unwind: '$orders' },
        //   { $match: { 'orders.orderCreationDate': {
        //     $gte: ISODate("2021-03-021T23:00:00.000Z"),
        //     $lt: ISODate("2021-03-022T23:00:00.000Z")
        // }}},
        // { $match: { 'orders.orderStatus': { $not: /In Cart/ }}},
        // { $match: { 'orders.orderCreationDate': $lt: ISODate("2021-03-022T23:00:00.000Z")}}},
        {$and:
          [
            { 'orders.orderCreationDate': {$gte: ISODate(`2021-03-10T20:41:00.051+00:00`)}},
             { 'orders.orderCreationDate': {$lte:ISODate(`2021-03-26T20:41:00.051+00:00`)}}
           ]
         }
        // {  $and: [
        //     {
        //         $gte: [ 'orders.orderCreationDate', ISODate(`2021-03-18T21:07:42.313+00:00`) ]
        //     },
        //     {
        //         $lte: [ 'orders.orderCreationDate', ISODate(`2021-03-24T21:08:42.313+00:00`) ]
        //     },
        // ]}

       ])

Все что написано выше не работает, прилетает пустой ответ . Как мне написать запрос, который проверяет orderCreationDate и находит все объекты между 2021-03-18 и 2021-03-24?

2
  • А агрегация тут у вас с какой-то особой, неозвученной в вопросе целью, или просто так вышло?
    – Yaant
    26 мар 2021 в 6:56
  • Я неособо разбираюсь в Монго, так сомо вышло
    – Volodimir
    26 мар 2021 в 11:45

1 ответ 1

0

Нужно было преоброзовать дату в формат который принимает Mongo

    const users = await User.aggregate  ([
      { $unwind: "$orders"},
      { $match:  {
      "orders.orderCreationDate": {
        $gte: new Date("2021-03-21T20:41:00.051+00:00"),
        $lte: new Date("2021-03-22T20:41:00.051+00:00")
      }}
    }}
   ])
1
  • По идее, с ISODate тоже должно работать. И судя по вопросу, Вам достаточно сделать просто const users = await User.find ({"orders.orderCreationDate": {$gte: new Date("2021-03-21T20:41:00.051+00:00"), $lte: new Date("2021-03-22T20:41:00.051+00:00")}} без всяких aggregate.
    – Yaant
    26 мар 2021 в 13:12

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.