Search code examples
c#mongodb-.net-drivermongodb-csharp-2.0

MongoDB CSharp In Filter Is Not Correct


I have a collection of Communication documents with an Int field called CommId and I am trying to find all matching documents that are contained in an array of ints using the code below. If this query worked, it should return 5 documents.

var filter = Builders<Communication>.Filter.In(x => x.CommID, CommunicationIds);
List<Communication> allComms = _context.Communications.Find(filter).ToList<Communication>();

CommunicationIds is the array of Ints and, as I step through the code, I see that it contains [1,2,3,4,5]. While stepping through the code, I have verified that the Communications collection has entries with all 5 of these documents (there are hundreds of documents overall) and that each of those documents has a CommID equal to 1,2,3,4,5.

Yet when I run this query, I get no results - something about my filter is not correct but I do not know why. Does anybody has any suggestions?


Solution

  • It turned out that MongoDB loaded the JSON documents as string values instead of integers. I had to get Navicat for MongoDB and then inspect the column value types. It was easy to correct once I knew the issue. Once all integer values were actually ints and not strings, the query worked fine.