Mongodb query only works without quotes

Viewed 24

I have the following data the collection outgoing_counts in a Mongo DB document.

    { "_id" : ObjectId("632158ed6fe76c381114ef71"), "MSISDN" : "0", "tot_out_duration" : 
     NumberLong(46), "num_out" : NumberLong(2), "tot_in_duration" : NumberLong(0), "num_in" : 
     NumberLong(0), "CALL_DATE_HOUR" : "2022091400" }
    { "_id" : ObjectId("632158ed6fe76c381114ef72"), "MSISDN" : "14169364496", 
     "tot_out_duration" : NumberLong(29), "num_out" : NumberLong(1), "tot_in_duration" : 
     NumberLong(0), "num_in" : NumberLong(0), "CALL_DATE_HOUR" : "2022091400" }
     { "_id" : ObjectId("632158ed6fe76c381114ef73"), "MSISDN" : "22393750115", 
      "tot_out_duration" : NumberLong(135), "num_out" : NumberLong(2), "tot_in_duration" : 
      NumberLong(0), "num_in" : NumberLong(0), "CALL_DATE_HOUR" : "2022091400" }
     { "_id" : ObjectId("632158ed6fe76c381114ef74"), "MSISDN" : "2250797667529", 
      "tot_out_duration" : NumberLong(0), "num_out" : NumberLong(0), "tot_in_duration" : 
      NumberLong(2), "num_in" : NumberLong(1), "CALL_DATE_HOUR" : "2022091400" }

When querying the data using:

    db.outgoing_counts.find({"CALL_DATE_HOUR":/20220914/})

It returns the correct results. However when we use this:

    db.outgoing_counts.find({"CALL_DATE_HOUR":"/20220914/"})

The result set is empty.

I am using the "" as the parameters in the query sent from PySpark always comes with quotes. Is there are a way to send the parameters to MongoDB from PySpark without quotes or is it possible to get the correct results from MongoDB using quotes also.

Also tried the regexMatch and match functions but only works without the quotes.

1 Answers

Using {"CALL_DATE_HOUR":/20220914/} will find all documents where the value of CALL_DATE_HOUR contains 20220914.

Using {"CALL_DATE_HOUR":"/20220914/"} will find all documents where the value of CALL_DATE_HOUR is exactly /20220914/.

The same will happen if you will try to find a number using a string.

If you want to use a string, you can use:

db.collection.find({
  "CALL_DATE_HOUR": {$regex: "20220914"}
})

See how it works on the playground example

Related