Как использовать функцию count и where в MongoDB?

Нужна помощь здесь .. застрял на некоторое время.

Перечислите все имена, названия компаний клиентов, которые подали более 2 кредитов с годовым планом погашения.

Я должен получить 3 результата: Джон Тан, Happy Food Industry Pte Ltd Мэри Ли, Nice Man Advertising Pte Ltd Юю Ли, Учебный центр NAGM

Спасибо....!

Мой набор данных:

      [
                {
                "name":"John Tan",
                "contact_number":["81726351","92019283","90192837"],
                "company_name":"Happy Food Industry Pte Ltd",
                "company_registration_number":950001819,
                "type_of_ownership":"owner",
                "last_roll_call":"16-06-21",
                "follow_up_notes":"Do not require any loan at this moment",
                "need_to_follow_up":"Yes",
                "plan_next_call":"01-12-21",
                "financial_year_ended":"30-06-21",
                "profit_and_lost_as_on_last_closing":8000.00,
                "cumulative_profit_and_lost_as_on_last_closing":50000,
                "loan_applied":["Purchase of delivery lorry","Renovation of tuition centre"],
                "commission_date":"01-12-19",
                "loan_duration_in_mths":24,
                "interest_rate":3.00,
                "repayment_plan":"yearly",
                "loan_amount":138058298.00,
                "status":"Requesting for delay payments",
                "other_notes":null},{"name":"Mary Lee","contact_number":["95213355"],"company_name":"Nice Man Advertising Pte Ltd","company_registration_number":960087657,"type_of_ownership":"Partner with 50% holding","last_roll_call":"15-06-21","follow_up_notes":"Do not require any loan at this moment","need_to_follow_up":"Yes","plan_next_call":"Dec 21","financial_year_ended":"30-01-21","profit_and_lost_as_on_last_closing":9000.00,"cumulative_profit_and_lost_as_on_last_closing":40000,"loan_applied":["Purchase of crane","Purchase of machinery from USA"],"commission_date":"01-11-19",
"loan_duration_in_mths":23,"interest_rate":4.00,"repayment_plan":"yearly","loan_amount":1237162.00,"status":"Healthy","other_notes":null},{"name":"Yuyu Lee","contact_number":["928371623"],"company_name":"NAGM Tuition Centre","type_of_ownership":"Partner with 20% holding","follow_up_notes":"Do not require any loan at this moment","need_to_follow_up":"Yes","plan_next_call":"12-12-21","financial_year_ended":"30-01-21","profit_and_lost_as_on_last_closing":1000000.00,"cumulative_profit_and_lost_as_on_last_closing":3002000,"loan_applied":["Purchase of raw material from China","Renovation of shopping mall"],"commission_date":"01-11-19","loan_duration_in_mths":11,"interest_rate":4.00,"repayment_plan":"yearly","loan_amount":918272.00,"status":"Completed","other_notes":null},{"name":"Joseph Lee","contact_number":["918273615","0192837176","1827361923"],"company_name":"Akaka Tuition Centre","company_registration_number":929381782,"type_of_ownership":"Director","last_roll_call":"15-11-21","follow_up_notes":"Do not require any loan at this moment","need_to_follow_up":"No","plan_next_call":null,"financial_year_ended":"30-01-21","profit_and_lost_as_on_last_closing":999999.00,
                "cumulative_profit_and_lost_as_on_last_closing":9000,"loan_applied":"Purchase of delivery lorry","commission_date":"01-11-19","loan_duration_in_mths":11,"interest_rate":4.00,"repayment_plan":"monthly","loan_amount":918272.00,"status":"Completed","other_notes":null},{"name":"Tan Meh Meh","contact_number":["92837162","9189278371"],"company_name":"Target Pte Ltd","company_registration_number":0293891823,"type_of_ownership":"Director","last_roll_call":"15-12-21","follow_up_notes":"Do not require any loan at this moment","need_to_follow_up":"Yes","plan_next_call":"09-12-21","financial_year_ended":"30-01-21","profit_and_lost_as_on_last_closing":12500000.00,"cumulative_profit_and_lost_as_on_last_closing":1500000,"loan_applied":"Purchase of delivery lorry","commission_date":"01-11-19","loan_duration_in_mths":55,"interest_rate":4.00,"repayment_plan":"monthly","loan_amount":918272.00,"status":"Completed","other_notes":null},{"name":"Teo Bee Bee","contact_number":["92039189278","192838182","192837172"],"company_name":"Hello Kitty Pte Ltd","company_registration_number":102039192,"type_of_ownership":"Owner","last_roll_call":"13-11-21","follow_up_notes":"Do not require any loan at this moment","need_to_follow_up":"Yes","plan_next_call":"09-01-21","financial_year_ended":"30-01-21","profit_and_lost_as_on_last_closing":6000.00,"cumulative_profit_and_lost_as_on_last_closing":24000000,"loan_applied":"Purchase of delivery lorry","commission_date":"01-11-19","loan_duration_in_mths":33,"interest_rate":4.00,"repayment_plan":"monthly","loan_amount":2312333.00,"status":"Requesting for delay payments","other_notes":null}]````

1 ответ

Следующий запрос даст вам то, что вам нужно.

      db.collection.find( { "loan_applied.1": {  "$exists": true }, "repayment_plan": "yearly" } , {"name":1, "company_name": 1} );

Объяснение:

С loan_appliedявляется полем массива, мы можем использовать точечную нотацию, чтобы увидеть, существует ли какой-либо элемент после индекса 1, что означает более 1 элемента. Добавим еще один фильтр для repayment_plan.

Во второй части запроса мы используем проекцию, чтобы вернуть только nameа также company_name.

Другие вопросы по тегам