我正在使用MongoDB Rust Prototype库,并且正在尝试创建一个聚集阶段。
如何将以下内容转换为Rust聚合阶段?
const aggregationStages = [{
"$match": {
"_id": {
"$in": [ObjectId("5b4289600000000000000000"), ObjectId("5b1afc600000000000000000")]
}
},
"$project": {
"_id": 1,
"field": 1,
"hours": [
{
"starts": {
"$arrayElemAt": ["$times.start", 0]
}
},
{
"ends": {
"$arrayElemAt": ["$times.end", -1]
}
}
]
}
}]
我想我不知道如何将数组转换为有效的Bson文档。
我尝试了以下操作,但有<bson macros>
错误。
// get the colelction from the db
let coll = client.db("mydb").collection("places");
let mut aggregation_stages: Vec<bson::Document> = vec![];
let mut aggregation_options = mongodb::coll::options::AggregateOptions::new();
aggregation_options.allow_disk_use = Some(true);
aggregation_options.batch_size = 1000;
let object_ids = ["5b4289600000000000000000", "5b1afc600000000000000000"];
let mut ids: Vec<bson::oid::ObjectId> = vec![];
for x in object_ids.iter() {
ids.push(bson::oid::ObjectId::with_string(x).unwrap())
}
aggregation_stages.push(doc! { "$match": doc! { "_id": doc! { "$in": ids} }});aggregation_stages.push(doc! {"$project": doc! {
"_id" : 1,
"other_field" : 1,
"hours": [
doc! { "starts" : doc! {"$arrayElemAt" : ["$times.start", 0]} },
doc! { "ends" : doc! {"$arrayElemAt" : ["$times.end", -1]} }
]
}});
我遇到以下错误:
error[E0277]: the trait bound `bson::Bson: std::convert::From<std::vec::Vec<bson::oid::ObjectId>>` is not satisfied
--> src\main.rs:111:60
|
111 | aggregation_stages.push(doc! { "$match": doc! { "_id": doc! { "$in": ids} }});
| ^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::vec::Vec<bson::oid::ObjectId>>` is not implemented for `bson::Bson`
|
= help: the following implementations were found:
<bson::Bson as std::convert::From<i32>>
<bson::Bson as std::convert::From<f32>>
<bson::Bson as std::convert::From<std::vec::Vec<bson::Bson>>>
<bson::Bson as std::convert::From<(bson::spec::BinarySubtype, std::vec::Vec<u8>)>>
and 16 others
= note: required by `std::convert::From::from`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)