1

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. :)

2
  • That would be easier with Linq i guess, but not sure if you can use it in your project.
    – Zorkind
    Jan 3, 2018 at 19:33
  • I've tried it with linq but it didn't work. you're focusing on my query. Its wrong. Mongodb c# driver throws an error if I use where clause inside where clause.
    – ChiragMS
    Jan 3, 2018 at 19:46

1 Answer 1

3

You can try with Not

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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