• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

197
Views
MongoDB and LINQ: "NOT IN" clause

I have two collections, one is a list of image names, the second is a subset of that list. When a task has been completed its name is inserted into the second collection.

I need to retrieve a set of not yet completed image names from the first collection. I have achieved this successfully with:

var processedNames = processed.AsQueryable().Select(x => x.ImageName).ToArray();
foreach (var result in results.Where(x => !processedNames.Contains(x.ImageName))

However this brings a large list of strings back from the database and then sends it back to the database in a single document, which as well as being inefficient will break eventually.

So I tried to rewrite it so it's all performed server side with:

            var results = from x in captures
                          join complete in processed.AsQueryable() on x.ImageName equals complete.ImageName into completed
                          where !completed.Any()
                          select x;

This fails with:

System.NotSupportedException: '$project or $group does not support {document}.'

I also tried using the non LINQ API:

                var xs = capturesCollection.Aggregate()
                    .Lookup("Processed", "ImageName", "ImageName", @as: "CompletedCaptures")
                    .Match(x => x["CompletedCaptures"] == null)
                    .ToList();

This fails with:

MongoDB.Bson.BsonSerializationException: 'C# null values of type 'BsonValue' cannot be serialized using a serializer of type 'BsonValueSerializer'.'

How can I achieve this query completely server side with the C# driver? A pure LINQ solution is preferable for portability.

about 3 years ago · Santiago Trujillo
1 answers
Answer question

0

I worked out how to do it with the Aggregate API:

                var results = capturesCollection.Aggregate()
                    .As<CaptureWithCompletions>()
                    .Lookup(processed, x => x.ImageName, x => x.ImageName, @as:(CaptureWithCompletions x) => x.CompletedCaptures)
                    .Match(x => !x.CompletedCaptures.Any())
                    //.Limit(2)
                    .ToList();
about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error