gpt4 book ai didi

regex - 在Rust中使用正则表达式查询MongoDB

转载 作者:行者123 更新时间:2023-12-03 11:37:00 33 4
gpt4 key购买 nike

我正在尝试将bucket pattern实现为previous question的解决方案。
在示例中,他们使用选择器发布更新,该选择器使用正则表达式:

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使用正则表达式查询?

最佳答案

我发现了这一点,因此对于将来遇到此问题的任何人:
mongodb驱动程序不使用Regex crate ,而是bson crate defines a 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(),
};
瞧,玛莎!

关于regex - 在Rust中使用正则表达式查询MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63890003/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com