首页 > 解决方案 > 在 Rust 中使用正则表达式查询 MongoDB

问题描述

我正在尝试实现存储桶模式作为上一个问题的解决方案。

在示例中,他们使用使用正则表达式的选择器发出更新:

db.history.updateOne({ "_id": /^7000000_/, "count": { $lt: 1000 } },
    { 
        "$push": { 
            "history": {
                "type": "buy",
                "ticker": "MDB",
                "qty": 25,
                "date": ISODate("2018-11-02T11:43:10")
            } },
        "$inc": { "count": 1 },
        "$setOnInsert": { "_id": "7000000_1541184190" }
    },
    { upsert: true })

我试图在 Rust 中做同样的事情,但查询将我的正则表达式解释为字符串文字,而不是评估正则表达式。

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RepetitionBucketUpdate {
    #[serde(with = "serde_regex")]
    id: Regex,
    device_id: Uuid,
    session_id: Uuid,
    set_id: Uuid,
    exercise: String,
    level: String,
    count: mongodb::bson::Bson,
}

impl From<JsonApiRepetition> for RepetitionBucketUpdate {
    fn from (value: JsonApiRepetition) -> Self {
        
        let id = format!("^{}_", value.device_id.to_string().replace("-", ""));
        let re = Regex::new(&id).unwrap();

        RepetitionBucketUpdate {
            id: re,
            device_id: value.device_id,
            session_id: value.session_id,
            set_id: value.set_id,
            exercise: value.exercise,
            level: value.level,
            count: mongodb::bson::bson!( { "$lt": BUCKET_RECORD_LIMIT }),
        }
    }
}

let update = bson::doc! {
    "$push": {
        "repetitions": mongodb::bson::to_bson(&repetition_update).unwrap(),
    },
    "$inc": { "count": 1 },
    "$setOnInsert": { "id": oid }
};

let options = mongodb::options::UpdateOptions::builder()
    .upsert(true)
    .build();

collection.update_one(query, update, options).await.map_err(CollectorError::DbError)?;

如果我println!看到更新参数:

query: Document({"id": String("^6fcd683c20d5415da1341e7d2f780749_"), "device_id": String("6fcd683c-20d5-415d-a134-1e7d2f780749"), "session_id": String("8388e24d-e680-46f4-9205-b9e43e39a17a"), "set_id": String("53d5a3ec-5962-402d-8e8a-41e9c5e3e01f"), "exercise": String("Bench Press"), "level": String("WheelsWithinWheels"), "count": Document(Document({"$lt": Int32(1000)}))})
update: Document({"$push": Document(Document({"repetitions": Document(Document({"number": Int32(88), "rom": Double(69.42), "duration": Double(666.0), "time": Int64(10870198172412)}))})), "$inc": Document(Document({"count": Int32(1)})), "$setOnInsert": Document(Document({"id": String("6fcd683c20d5415da1341e7d2f780749_1600107371599537000")}))})
options: UpdateOptions {
    array_filters: None,
    bypass_document_validation: None,
    upsert: Some(
        true,
    ),
    collation: None,
    hint: None,
    write_concern: None,
}

它不匹配,它将每个更新作为新文档插入,而不是对后续更新进行存储。

我可以从 mongodb shell 成功发出基于正则表达式的查询。如示例中所示,如何使用 Rust 和 mongodb 使用正则表达式进行查询?

标签: regexdatabasemongodbrust

解决方案


我想通了,所以对于将来遇到此问题的任何人: mongodb 驱动程序不使用 Regex 板条箱,而是bson 板条箱定义了 Regex struct

我的用法从上面(见问题)更改为:

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RepetitionBucketUpdate {
    pub id: mongodb::bson::Bson,
    pub device_id: Uuid,
    session_id: Uuid,
    set_id: Uuid,
    exercise: String,
    level: String,
    count: mongodb::bson::Bson,
}

let id = format!("^{}_", value.device_id.to_string().replace("-", ""));
let re = mongodb::bson::Regex {
    pattern: id,
    options: String::new(),
};

瞧,ca Marche!


推荐阅读