Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1824
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T03:40:06+00:00 2022-05-31T03:40:06+00:00

mongodb – Query an array of linked documents in Mongo using aggregates

  • 0

[ad_1]

I have the following collections.

interface PartnerOrganization extends Document {
    retailLocations?: RetailLocations[];
}

interface RetailLocation extends Document {
    location: {
        type: string;
        coordinates: [number];
    };
}

RetailLocation has an appropriate index setup, and I can successfully run aggregate queries such as the following

 const retailLocation = await RetailLocation.aggregate([
          {
            $geoNear: {
              near: { type: 'Point', coordinates: [searchInput.longitude, searchInput.latitude] },
              distanceField: 'dist.calculated',
              maxDistance: Number(searchInput.radius),
              includeLocs: 'dist.location',
              spherical: true,
            },
          },
        ]);

I would like to be able to write an aggregate pipeline which uses a $geoNear aggregate function, against PartnerOrganizations.

The overall goal being that I can do a search for PartnerOrganizations that match certain $search (I am using the $search aggregate) criteria as well as location criteria.

The issue I am having is that $geoNear needs to be the first step in the aggregate pipeline, and as such retailLocations hasn’t been populated at that stage. I have tested it out with $lookups, but doing anything before the $geoNear stage will cause a Mongo error similar to this –

enter image description here

Basically, my ‘dream query’ if you will, is something like this

 const retailLocation = await PartnerOrganization.aggregate([
        {
          $lookup: {
            from: 'retaillocations',
            localField: 'retailLocations',
            foreignField: '_id',
            as: 'retailLocations',
          },
        },
        {
          $geoNear: {
            key: 'retailLocations.location',
            near: { type: 'Point', coordinates: [searchInput.longitude, searchInput.latitude] },
            distanceField: 'dist.calculated',
            maxDistance: Number(searchInput.radius),
            includeLocs: 'dist.location',
            spherical: true,
          },
        },
 {
          $search: {
            index: 'Partner Organizations',
            text: {
              query: `*${searchInput.text}*`,
              path: ['companyName', 'instagramBio', 'ogDescription'],
            },
          },
        }
      ]);

But just not sure if this is possible, or if i have to do separate queries to make it work, then filter/map between the results. Just for claritys sake – I am using Linked Documents, not embedded – you can see what that looks like in Compass below.

enter image description here

[ad_2]

  • 0 0 Answers
  • 9 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 5380 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1169 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1111 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.