ChiragMS
ChiragMS

Reputation: 555

Mongodb C# complex query

I'm kind of new to c# mongodb driver.

I have a collection which contains documents with following structure.

{
  _id          : 5a424d61f5213516a0249323,
  email        : abc@gmail.com,
  applications : [
     {
        applicationId    : 5a3b54723629c20df8bcae8d,
        applicationStatus: "Open",
        type             : "Private",
        category         : "Tech"
     },
     {
        applicationId    : 5a3b54723629c20df8bcaeasd,
        applicationStatus: "Close",
        type             : "Public",
        category         : "Agri"
     },
     {
        applicationId    : 5a3b54723629c20df8bcajkl,
        applicationStatus: "Open",
        type             : "Public",
        category         : "Business"
     },
     {
        applicationId    : 5a3b54723629c20df8bca852,
        applicationStatus: "Close",
        type             : "Public",
        category         : "Agri"
     },
  ]
}

I want to get all the documents which doesn't contain an application with category "Tech". In simple terms, I want all documents except the ones with application of category "Tech".

I've tried following query,

db.users.Find(Builders<UserModel>.Filter.And(
    Builders<UserModel>.Filter.ElemMatch(u => applications, a => a.category!= "Tech")
))

But it returns blank query Can someone teach me how to write complex query for this type of documents? Ignore my query, Suggest me standard way to get this done.

Thanks in advance. :)

Upvotes: 1

Views: 1434

Answers (1)

RICKY KUMAR
RICKY KUMAR

Reputation: 673

You can try with Not

db.users.Find(Builders<UserModel>.Filter.Not(Builders<UserModel>.Filter.ElemMatch(u => applications, a => a.category == "Tech")))

Upvotes: 3

Related Questions