【问题标题】:mongoose: find value inside object, inside array inside array of objects猫鼬:在对象内查找值,在对象数组内的数组内查找
【发布时间】:2020-12-16 18:15:15
【问题描述】:

您好,抱歉标题, 很难用语言来解释。

我有一个多级数据:

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

我正在尝试请求文档,在 wSent 内部,在 strategyParameters 内部,confirmationCandle 时间戳大于某个值。 我已经尝试了几次类似的事情:

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

但它没有成功,任何帮助将不胜感激。

下面是我的数据库中文档的具体示例:

{
"_id" : ObjectId("5fd7b0f1356b89312949963a"),
"email" : "test@test.com",
"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"
            }
        ]
    }
]
}

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    您的查询不匹配,因为您要搜索的字段与文档的结构不一致。示例文档没有exchangepairtimeframewSent.confirmationCandle.timestamp 字段。但它确实strategyParameters.wSent.exchangestrategyParameters.wSent.pairstrategyParameters.wSent.timeframestrategyParameters.wSent.confirmationCandle.timestamp 字段。

    您的查询应如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 2016-02-05
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      相关资源
      最近更新 更多