MongoDB aggregate returns nothing

Hello!

I am running mongoDB Atlas and I am trying to do an aggregate request to my cluster. If go to mongoDB Atlas and try to perform the query on the webpage, it works just fine.

Here's the query:

{
  $search: {
    index: "default",
    search: {
      query: "Test",
      path: "name"
    }
  }
}

However, when running my app the search returns nothing. Here's the code I use in Rust:

if let Ok(mut r) = collection.aggregate(search).await {
    let mut results = Vec::<(String, String)>::new();
    while let Some(d) = r.next().await {
        println!("Search success!");
        if let Ok(d) = d {
            let id = d.get_object_id("_id").unwrap();

What's more interesting is that if I put a breakpoint on the top row and then run it step by step, I actually do get a result and it prints "Search success!". Anyone have any ideas what's causing this?

Thanks,
Gustav

This looks like a problem with concurrency:

  • you are using async-await
  • the timing affects the availability of the results (wait for longer in the debugger -> get the result).

Did you try using the synchronous interface to check if it has the same problem? Maybe there's a concurrency logic bug either in your code or in the MongoDB driver.

1 Like

I didn’t try the sync features to see if this changes, but what could it be in my code that would cause this issue?

It only seems to happen with aggregate requests and using an Atlas search index, not when doing standard mongodb requests.

Thanks,
Gustav