0

I am a novice so I apologize but I keep getting this CS1061 error when trying to await a .find() method. How do I await this find()?

MongoClient client = new MongoClient(mongoDBSettings.Value.ConnectionURI);
        IMongoDatabase database = client.GetDatabase(mongoDBSettings.Value.DatabaseName);
        _userCollection = database.GetCollection<User>(mongoDBSettings.Value.CollectionName);
        _partyCollection = database.GetCollection<Party>(mongoDBSettings.Value.CollectionName);
    

public async Task<IEnumerable> GetNearbyParties(string postalCode) {

        var nearbyParties = await _partyCollection.Find(x => x.Address.postalCode == postalCode);

        return (IEnumerable<Party>)nearbyParties;

    }

MongoClient client = new MongoClient(mongoDBSettings.Value.ConnectionURI); IMongoDatabase database = client.GetDatabase(mongoDBSettings.Value.DatabaseName); _userCollection = database.GetCollection(mongoDBSettings.Value.CollectionName); _partyCollection = database.GetCollection(mongoDBSettings.Value.CollectionName);

public async Task<IEnumerable> GetNearbyParties(string postalCode) {

        var nearbyParties = await _partyCollection.Find(x => x.Address.postalCode == postalCode);

        return (IEnumerable<Party>)nearbyParties;

    }

I had it originally set up like this running synchronously: public async Task<IEnumerable> GetNearbyParties(string postalCode) {

        var nearbyParties = _partyCollection.Find(x => x.Address.postalCode == postalCode);

        return (IEnumerable<Party>)nearbyParties;

      
      



        //return results;
    }

But I understand that since it's an async method I should have an await when I try to search the database so that other things can run while that is fetched.

2

1 Answer 1

-1

You need to call asynchronous API of Mongo not normal Find method:

public async Task<IEnumerable> GetNearbyParties(string postalCode) {
    
            var nearbyParties = await _partyCollection.FindAsync(x => x.Address.postalCode == postalCode);
    
            return (IEnumerable<Party>)nearbyParties;
    
        }
5
  • it's not really true, Find also kind of supports awaiting, the only note that Find itself creates only cursor that doesn't call any actual command, you should call await cursor.ToListAsync() or similar logic
    – dododo
    Commented Jul 12, 2022 at 21:27
  • @dododo maybe yes or maybe not but with the Find method directly you cannot use await and that is the real question or not ??? besides being a bad practice what you say. If there is a FindAsync method, the correct thing to do is to use that method. Commented Jul 12, 2022 at 21:31
  • the question what was done incorrectly with Find, it's incorrect to say: don't use Find and use something else.
    – dododo
    Commented Jul 12, 2022 at 21:46
  • not something else. FindAsync is the same thing that Find . But the correct way to work asynchronously is with FindAsync. Commented Jul 12, 2022 at 22:41
  • 1
    Find and FindSync/FindAsync are different methods that works in different ways
    – dododo
    Commented Jul 12, 2022 at 22:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.