Alex Launi
Alex Launi

Reputation: 443

Querying MongoDB with a regular expression in Rust

I am trying to implement the bucket pattern as solution to a previous question.

In the example they issue an update with a selector that uses a regular expression:

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 })

I'm trying to do the same in Rust, but the query is interpreting my regex as a string literal and not evaluating the regex.

#[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)?;

If I println! the update parameters I see:

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,
}

It's not matching, and it's inserting every update as a new document rather than bucketing subsequent updates.

I can successfully issue a regex based query from the mongodb shell. How do I query with a regex using Rust and mongodb as in the example?

Upvotes: 3

Views: 1687

Answers (1)

Alex Launi
Alex Launi

Reputation: 443

I figured this out, so for anyone who has this problem in the future: The mongodb driver does not use the Regex crate, instead the bson crate defines a Regex struct.

My usage changed from the above (see question) to:

#[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(),
};

And voilà, ça marche!

Upvotes: 7

Related Questions