AdilA
@AdilA
Нравится кодить, изучаю go c echo

Как из mongodb запросить определные fileds?

Добрый времени суток!

Есть array внутри array есть еще один array, как запросить из второго array range по датам?

{
      "id": "6295001bef176110cb52076c",
      "companyID": "6294deeed16a4491020a6f3a",
      "createdAt": "2022-05-30T17:34:19.458Z",
      "updatedAt": "2022-05-31T12:23:58.805Z",
      "apoinments": [
        {
          "id": "62a349a1fe907103f8d33111",
          "startTime": "2022-06-10T12:00:00Z",
          "endTime": "2022-06-10T14:00:00Z",
          "createdAt": "2022-06-10T13:39:45.057Z",
          "companyID": "6294deeed16a4491020a6f3a",
          "bookingItemID": "6295001bef176110cb52076c",
        },
        {
          "id": "62a349c0fe907103f8d33112",
          "startTime": "2022-06-10T14:00:00Z",
          "endTime": "2022-06-10T16:00:00Z",
          "createdAt": "2022-06-10T13:40:16.927Z",
          "companyID": "6294deeed16a4491020a6f3a",
          "bookingItemID": "6295001bef176110cb52076c",
        }
      ],
      "title": "One",
    }, 
{
      "id": "6295001bef176110cb52076c",
      "companyID": "6294deeed16a4491020a6f3a",
      "createdAt": "2022-05-30T17:34:19.458Z",
      "updatedAt": "2022-05-31T12:23:58.805Z",
      "apoinments": [
        {
          "id": "62a349a1fe907103f8d33111",
          "startTime": "2022-06-10T12:00:00Z",
          "endTime": "2022-06-10T14:00:00Z",
          "createdAt": "2022-06-10T13:39:45.057Z",
          "companyID": "6294deeed16a4491020a6f3a",
          "bookingItemID": "6295001bef176110cb52076c",
        },
        {
          "id": "62a349c0fe907103f8d33112",
          "startTime": "2022-06-10T14:00:00Z",
          "endTime": "2022-06-10T16:00:00Z",
          "createdAt": "2022-06-10T13:40:16.927Z",
          "companyID": "6294deeed16a4491020a6f3a",
          "bookingItemID": "6295001bef176110cb52076c",
        }
      ],
      "title": "Two",


это то что нужно из второго array

В колекции выстроено следующим образом

Company{
BookingItems: {
Object{
Apoinments{
Object{
},
Object{
}
}
},
Object{
Apoinments{
Object{
},
Object{
}
}
}
}
}


Сейчас функция выглядит так:

func (companyRepository *companyRepositoryImpl) GetCompanyBookingItems(companyID string) (*model.CompanyBookingItems, error) {

	var existingCompany model.Company
	objectId, _ := primitive.ObjectIDFromHex(companyID)
	filter := bson.M{"_id": objectId}

	err := companyRepository.Connection.Collection("companies").FindOne(cntx, filter).Decode(&existingCompany)
	if err != nil {
		return nil, exception.ResourceNotFoundException("Company", "id", companyID)
	}

	switch hour := time.Now().Hour(); { // missing expression means "true"

	// dev
	case hour >= 8 && hour <= 23:
		// prod
		// case hour >= 3 && hour <= 18:
		// log.Println("hour", hour)
		startDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 11, 0, 0, 0, time.UTC)
		endDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()+1, 05, 0, 0, 0, time.UTC)

		bookingItems := *existingCompany.BookingItems

		for i := range bookingItems {

			bookingItem := bookingItems[i]

			var newApoinments []model.Apoinment

			for _, apoinment := range *bookingItem.Apoinments {

				if apoinment.StartTime.Local().After(startDate.Local()) && apoinment.EndTime.Local().Before(endDate.Local()) {
					newApoinments = append(newApoinments, apoinment)
					// log.Println("10 - 23", apoinment.ClientName)
				}
			}

			*bookingItems[i].Apoinments = newApoinments
		}

	// dev
	case hour >= 0 && hour <= 7:
		// prod
		// case hour >= 19 && hour <= 2:

		startDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, 11, 0, 0, 0, time.UTC)
		endDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 05, 0, 0, 0, time.UTC)
		// log.Println("start", startDate)
		// log.Println("end", endDate)

		bookingItems := *existingCompany.BookingItems

		for i := range bookingItems {

			bookingItem := bookingItems[i]

			var newApoinments []model.Apoinment

			for _, apoinment := range *bookingItem.Apoinments {
				if apoinment.StartTime.Local().After(startDate.Local()) && apoinment.EndTime.Local().Before(endDate.Local()) {
					newApoinments = append(newApoinments, apoinment)
					// log.Println("10 - 23", apoinment.ClientName)
				}
			}

			*bookingItems[i].Apoinments = newApoinments
		}
	}

	return &model.CompanyBookingItems{
		Data: *existingCompany.BookingItems,
	}, nil
}


но на production выходит ошибка runtime error: invalid memory address or nil pointer dereference

с agregate так и не разобрался...
  • Вопрос задан
  • 89 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы