How to filter multiple conditions with single aggregation pipeline

Hi Team,
I am trying to read data from databricks but I would like to filter the data with 2 conditions .

  1. Filtering based on location – am getting filtered data
  2. filter the data based on date ( current date - 365 days or 1 year) - date is timestamp format.

Would like to add these 2 conditions in aggregation pipeline and need inputs.

Hi @Sai_Saran_Gangisetty welcome to the community!

I am trying to read data from databricks but I would like to filter the data with 2 conditions

Could you elaborate on what you need exactly? Databricks is a product that is not created by MongoDB, so I wonder if you’re in the right forum :slight_smile:

Would like to add these 2 conditions in aggregation pipeline and need inputs.

If you need help with MongoDB queries specifically, could you post an example document and the desired output? Very helpful if you can also post what you have tried already.

Best regards
Kevin

1 Like

Thanks Kevin for the reply and my intention is like need to filter 2 fields in the same collection, 1 is string type and am able to successfully do it, but another filter is like current date to last 1 year.

Filter:1
pipeline=[{’$match’: { ‘chem.code’:‘bbnehuyd’ }}]

pipeline=[{’$match’: { ‘chem.code’:‘bbnehuyd’ }}]

This is a typical MongoDB aggregation pipeline for matching documents with a certain condition.

another filter is like current date to last 1 year.

With regard to your question, unfortunately I still don’t have all the information I need to be able to help you. Perhaps you’re looking for something like this?

{$match: {'chem.code': <some condition>, <some date field>: <some date condition>}}

Could you post the relevant example documents, and what’s the desired output?

Best regards
Kevin

1 Like

Exactly and my date field in timestamp. Not sure how to filter with 365 days or 1 year like that.

try $dayOfYear https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfYear/#-dayofyear--aggregation-

1 Like

Thanks Ram for the help, but am not able to apply 2 filters at a time in aggregation pipelines. Like am getting nothing data from Mongo but am getting data with one filter.

please share your query and some sample data.

var start = new Date(new Date().setFullYear(new Date().getFullYear() - 1));  //Minus One Year 
start.setHours(0,0,0,0); //Set to Start of the Day

var end = new Date();
end.setHours(23,59,59,999); //Set to End of the Day
print(start, end)

db.collection.aggregate()
.match({country: "Australia", created_date: {$gte: start, $lt: end}});
1 Like

Hi @Sai_Saran_Gangisetty I would reitrate @psram 's request for an example document that you’re working with.

How a MongoDB query looks like would depend entirely on the document to be queries (structure, field names, datatypes on those fields, etc.). This is similar to how you would be required to know how an SQL relational schema look like and how each table connects to each other before you can create a query. Just as it’s impossible to create an SQL query without knowing the schema, it is impossible to create a MongoDB query without knowing the example document.

So in order to help you further, please post an example document, and the desired result in as much details you can.

Best regards
Kevin

1 Like