• 请不要在回答技术问题时复制粘贴 AI 生成的内容
YYYbw
V2EX  ›  程序员

MongoDB 的 Criteria 方法求助

  •  
  •   YYYbw · Oct 12, 2021 via Android · 2034 views
    This topic created in 1712 days ago, the information mentioned may be changed or developed.
    求助下大佬们,MongoDB 有没有调用 criteria 里的什么方法可以判断一个属性是否同时满足多个条件的
    现在想到的是 c.and().in() ,但是这个 in 字句还是不能支持同时满足
    5 replies    2021-10-12 18:20:09 +08:00
    AllanAG
        1
    AllanAG  
       Oct 12, 2021
    一个字段满足多个条件? 是范围查询吗。试试把条件拆开:and 中添加多个相同的字段,每个字段不同的条件。
    代码里是 c.and(c1,c2...)
    ```
    db.xxxx.find({
    $and:[{
    status:{
    $gte:"2"
    }
    },{
    status:{
    $lte:"3"
    }
    }],
    })
    ```
    qW7bo2FbzbC0
        2
    qW7bo2FbzbC0  
       Oct 12, 2021
    criteria 是什么,你的后半句,楼上说的对
    GiftedJarvis
        3
    GiftedJarvis  
       Oct 12, 2021
    用 `or`,像这样
    ```Kotlin
    val criteria = Criteria().orOperator(
    Criteria("1").`is`(1),
    Criteria("2").`is`(2)
    )
    mongoTemplate.find(
    Query(criteria),
    Any::class.java
    )
    ```
    GiftedJarvis
        4
    GiftedJarvis  
       Oct 12, 2021
    @GiftedJarvis 不对,是 `andOperator`
    YYYbw
        5
    YYYbw  
    OP
       Oct 12, 2021 via Android
    刚刚查了一下貌似有个 elematch 是同时满足的一个语句,返回一个 criteria 对象
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2450 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 11:48 · PVG 19:48 · LAX 04:48 · JFK 07:48
    ♥ Do have faith in what you're doing.