Mongodb C# complex query

岁酱吖の 提交于 2019-12-11 16:34:40

问题


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


回答1:


You can try with Not

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


来源:https://stackoverflow.com/questions/48083603/mongodb-c-sharp-complex-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!