1

I have Docs in mongo DB in the following format - Here outerTimestamp is in string format and Price is int format

{
   "_id":"ObjectId(""622f950e73043487031bb3ee"")",
   "outerTimeStamp" : "14-Mar-2022",
   "filtered":{
      "data":[
         {
            "Price":14350,
            "expiryDate":"17-Mar-2022",
            "info1":{
               "Price":14350,
               "expiryDate":"17-Mar-2022",
            },
            "info2":{
               "Price":14350,
               "expiryDate":"17-Mar-2022"
            }
         },
         {
            "Price":14350,
            "expiryDate":"17-Mar-2022",
            "info1":{
               "Price":14350,
               "expiryDate":"17-Mar-2022",
            },
            "info2":{
               "Price":14350,
               "expiryDate":"17-Mar-2022"
            }
         },
         ......
         ....
     ]
    }
}

My requirement is I need all the list elements [named data] having price == 14350 and anemphasized text entire document should be having date less than outerTimeStamp (17-Mar-2022)

I am using the following query but it not giving me the desired output.

data = db.find({
    '$and' : 
            [
                {'outerTimeStamp ': { '$lt': "15-Mar-2022" }},
                {'filtered.data': { '$elemMatch': { 'Price': 14350 }}}
            ]
        }, 
    {'filtered.data' : 1}
)

I did following command as well but this one is not returning anythign at all -

 import dateutil
dateStr = '2022-03-15T00:00:00.000Z'
myDatetime = dateutil.parser.parse(dateStr)

data = db.find({
    '$and' : 
            [
                {'outerTimestamp': { '$lt': myDatetime }},
                {'filtered.data.Price': 15000 }
            ]
        }, 
    {'filtered.data' : 1})

by this still, I am getting all the data elements, though this query is respecting outerTimeStamp condition [ {'outerTimeStamp ': { '$lt': "15-Mar-2022" }} ]

I will be grateful for the help

regards

2 Answers 2

2

Your query just needs a small change:

data = db.find({
'$and' : 
        [
            {'outerTimeStamp ': { '$lt': "15-Mar-2022" }},
            {'filtered.data': { '$elemMatch': { 'Price': 14350 }}}
        ]
    }, 
    {'filtered.data.$' : 1}
)

Ref: https://docs.mongodb.com/manual/reference/operator/projection/positional/#mongodb-projection-proj.-

Sign up to request clarification or add additional context in comments.

Comments

1

try with this

data = db.find({
      '$and' : [{'outerTimeStamp': { '$lt': "15-Mar-2022" }},
                {'filtered.data.Price': 14350 } ]}, 
      {'filtered.data' : 1})

3 Comments

Hi @MauriRamone thanks for your input but this one I have tried already and it also does not provide the required output.
Hi, I have noted that outerTimeStamp is set as String: "14-Mar-2022". Try with Date format, for example ISODate("2022-03-15T00:00:00.000Z")
nope! it does not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.