Alexis Brunet
Alexis Brunet

Reputation: 355

mongoose: find value inside object, inside array inside array of objects

Hello and sorry for the title, it is quite hard to explain with words.

I have a data with multiples level :

-strategyParameters -> [wsent -> [ {confirmationCandle.timestamp} ] ]

I am trying to request documents, having inside wSent, inside strategyParameters, a confirmationCandle timestamp greater than a certain value. I have tried several attemps with some things similar to this :

db.getCollection('users').find({"exchange":"binance","pair":"LTCUSDT","timeframe":"1h","wSent.confirmationCandle.timestamp":{"$gt":1606644800000}})

But it was unsuccessful, any help would be much appreciated.

bellow a concrete example of document inside my db:

{
"_id" : ObjectId("5fd7b0f1356b89312949963a"),
"email" : "[email protected]",
"password" : "$2b$10$egeg",
"strategyParameters" : [ 
    {
        "_id" : ObjectId("5fd7c9940d0f3033fc527547"),
        "pair" : "LTCUSDT",
        "strategy" : "w",
        "timeframe" : "1h",
        "exchange" : "binance",
        "wSent" : [ 
            {
                "_id" : ObjectId("5fd7adb9d157b430a05a87b1"),
                "firstBottom" : {
                    "open" : 79.46,
                    "high" : 80.07,
                    "low" : 78.29,
                    "close" : 78.91,
                    "timestamp" : 1606690800000.0,
                    "isTop" : false,
                    "isBottom" : true
                },
                "top" : {
                    "open" : 78.89,
                    "high" : 80.5,
                    "low" : 78.87,
                    "close" : 79.7,
                    "timestamp" : 1606694400000.0,
                    "isTop" : true,
                    "isBottom" : false
                },
                "seconBottom" : {
                    "open" : 79.73,
                    "high" : 79.84,
                    "low" : 78.55,
                    "close" : 79.29,
                    "timestamp" : 1606698000000.0,
                    "isTop" : false,
                    "isBottom" : true
                },
                "confirmationCandle" : {
                    "open" : 81.56,
                    "high" : 85,
                    "low" : 81.1,
                    "close" : 83.24,
                    "timestamp" : 1606744800000.0, <-- the target
                    "isTop" : false,
                    "isBottom" : false
                },
                "exchange" : "binance",
                "pair" : "LTCUSDT",
                "timeframe" : "1h"
            }
        ]
    }
]
}

Upvotes: 1

Views: 302

Answers (1)

Montgomery Watts
Montgomery Watts

Reputation: 4034

Your query doesn't match because the fields you're searching for don't align with the document's structure. The sample document doesn't have exchange, pair, timeframe or wSent.confirmationCandle.timestamp fields. But it does have strategyParameters.wSent.exchange, strategyParameters.wSent.pair, strategyParameters.wSent.timeframe and strategyParameters.wSent.confirmationCandle.timestamp fields.

Your query should look something like this:

db.getCollection("users").find({
  "strategyParameters.wSent.exchange": "binance",
  "strategyParameters.wSent.pair": "LTCUSDT",
  "strategyParameters.wSent.timeframe": "1h",
  "strategyParameters.wSent.confirmationCandle.timestamp": { $gt :1606644800000 }
})

Upvotes: 1

Related Questions