How to combine these two quieries into one quiery?

I want to combine these two queries into one. I tried with $search: { compound: ... but not working. I use mongo-atlas.

This query for finding opening dates in particular weekday

    const cursor = collections.Restaurants!.find({
        $and: [
            { 'timeSlot.1.opening': { $lte: new Date("2022-01-01T08:40:00.000Z") } },
            { 'timeSlot.1.closing': { $gte: new Date("2022-01-01T10:30:00.000Z") } },
        ],
    });

This is autocomplete query

  const agg = [
        {

            $search: {
                autocomplete: {
                    query: _req.params['title'], path: "title",
                    fuzzy: {
                        maxEdits: 1
                    }
                }
            },

        },
        { $limit: 10 },
        { $project: { _id: 0, title: 1 } }
    ];
    const resturants = collections.Restaurants!.aggregate(agg)

And I just want to is Realm better than Atlas for large scale quieries?

Hi @Bhanuka_Isuru - Welcome to the community :wave:

I presume you are trying to combine the queries you mentioned in your post. In that case, have you tried using a $match stage for your first query within the aggregation pipeline? For e.g.:

const agg = [
        {

            $search: {
                autocomplete: {
                    query: _req.params['title'], path: "title",
                    fuzzy: {
                        maxEdits: 1
                    }
                }
            },

        },
        {
        	$match: {
        		$and: [
            { 'timeSlot.1.opening': { $lte: new Date("2022-01-01T08:40:00.000Z") } },
            { 'timeSlot.1.closing': { $gte: new Date("2022-01-01T10:30:00.000Z") } },
        		]
        	}
        },
        { $limit: 10 },
        { $project: { _id: 0, title: 1 } }
    ]

Please note that i’ve yet to test this query out so I would recommend running it in a test environment to see if it works for you

Also, the above may only work if the first query you provided can match on documents output from the $search stage of the pipeline.

Additionally, if the above query does not help, please provide the following information:

  1. MongoDB version in use
  2. Sample input documents
  3. Expected output documents
  4. Output from the two queries you have provided

And I just want to is Realm better than Atlas for large scale quieries?

Do you have further context / information on your use case that you could provide regarding the large scale queries?

Regards,
Jason

1 Like

Thanks. I tried this before but I used $match with $search. Thanks again.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.