pyMongo - MongoDB query with multiple conditions on two fields

Hello,
firstly, I would like to thank you for the incredibly good support here in the community.
I’ve asked 2 questions and received an answer to both that have helped me move forward with my project Fantastic!

Now to my new Problem.

I make a query on a collection and give me all documents from the field “status_create” that have the value False. That works great too, but now I need to append another condition which is not so easy for me, and that is, I have a field called Timestamp which has “2023-04-23 20:00:37” such values.

The question now is, how do i get all the documents with “status_create=False” and the date, I don’t care about the time. Only the date should be correct, the date is the current date from the Day and will come from the VAR

Here is my func for the query.

def status_false(collections_name):
    
    current_date = time.strftime('%Y-%m-%d', time.localtime())
    status_create = collections_name.find({"status_create": "False"})
   
    status_create_False = []
    for data in status_create:
        status_create_False.append(data)

    return status_create_False

I hope this was understandable.

And one last question, is it useful to create the field “status_create” as a real bool ? (now is a String) if so, what advantages does this have.

Hey @Rainer_Schmitz,

It’s great to hear that the answers provided were helpful for your project, this shows the real power of community. :heart:

Coming back to your first question, if I understand this correctly, you want to get all the documents with status_create: False and the date from the Timestamp field without the time if it matches your current_date. Please correct my understanding if wrong. If this is indeed correct, you can use aggregation to do something like this:

db.test.aggregate([{
  $match: {
    status: false,
    Timestamp: {
      $gte: '2023-05-03',
      $lt: '2023-05-04'
    }
  }
}])

What the above code is doing is matching where the string is larger than the date in question and less than the date of the next day due to how string comparison work, you should see this query return the correct documents.

Please note that since I do not know your sample documents or your exact use case, I could only test the aggregation part on my end to see whether it was working or not. I created a sample collection having the following documents:

[
  {
    _id: ObjectId("6452462b566b22ff1630e55a"),
    Timestamp: '2023-05-03 20:00:37',
    status: false
  },
  {
    _id: ObjectId("645248d2566b22ff1630e55c"),
    Timestamp: '2023-05-03 21:10:17',
    status: false
  },
  {
    _id: ObjectId("64524903566b22ff1630e55d"),
    Timestamp: '2023-05-01 20:00:37',
    status: true
  },
  {
    _id: ObjectId("645305c0dfcb4b65f6697098"),
    Timestamp: '2023-05-03 20:00:37',
    status: true
  }
]

I then created a connection and used the following code:

db.test.aggregate([ {$match: {status:false, Timestamp:{$gte:'2023-05-03', $lt:'2023-05-04'}}} ])

This returned the expected documents:

[
  {
    _id: ObjectId("6452462b566b22ff1630e55a"),
    Timestamp: '2023-05-03 20:00:37',
    status: false
  },
  {
    _id: ObjectId("645248d2566b22ff1630e55c"),
    Timestamp: '2023-05-03 21:10:17',
    status: false
  }
]

Please note, that using indexes will help improve performance even more.

Definitely use the right data type. A boolean can be represented by a single bit, but the string False and True need much more bytes, wasting space. Also, you can get into trouble when you mistype Flase or Ture, while you get no ambiguity when you use real boolean values.

Hope this helps. Please feel free to reach out for anything else as well.

Regards,
Satyam

Hello Satyam,
many thanks for your answer, I have found a other solution

 current_date = time.strftime('%Y-%m-%d', time.localtime())

    #DocCOM: "^" Regex-Muster
    regex_pattern = "^" + current_date
    #DocCOM: mongoDB abfrage mit bedienungen auf 2 feldern und $regex
    status_create = collections_name.find({
        "status_create": "False",
        "timestamp": {"$regex": regex_pattern}
    })

    status_create_False = []
    for data in status_create:
        status_create_False.append(data)


    return status_create_False