Querying ObjectId

In context of dot net core c# Linq queries
I want to query for part of the object Id.
The ID is represented in Mongodb as object and in POCO as string.
Query the full Id works fine but the following does not work

var result= await _dataServiceJobONSITE.QueryAsync(j => j.Id.Contains("a")).ConfigureAwait(false);

where QueryAsync is

  public async Task<IList<T>> QueryAsync(Expression<Func<T, bool>> predicate)
        {
            try
            {
                //var result = await Collection.FindAsync(predicate).ConfigureAwait(false);
                var result = Collection.AsQueryable().Where(predicate);
                var batch = await result.ToListAsync().ConfigureAwait(false);

                return batch;
            }
            catch (Exception ex)
            {
                return new List<T>() { };
            }
        }

The _id in Mongo isn’t an Object but an ObjectId, it’s a unique identity that auto-increments, they are generated in a certain way which allows you to guarantee that certain parts will look the same (see: https://docs.mongodb.com/manual/reference/method/ObjectId/)

As such the only thing I can think of without testing is do a query that looks for a range if you want specific documents where you fill in the first part of the ObjectId and replace the later section with 0, an example I found on StackOverFlow that should work: mongodb - mongo objectid 'contains' query - Stack Overflow

Alternatively, you’d need to return all documents then parse the Id in code and extract only the documents you actually want, not great but using IAsyncEnumerable you could keep the memory overhead down by processing in batches…

It might be someone else has a better answer, but that’s my understanding of things :slight_smile: