RavenDB查询不包含

时间:2011-12-08 10:23:55

标签: ravendb

我收集了文档用户

User
{
  "Status": "ACTIVE",
  "Login": {
    "UserName": "login",
    "Password": null,
    "CreationDate": "2011-12-07T11:30:24.4062500Z",
    "Roles": [
      {
        "Id": "roles/WebUser",
        "Name": "WebUser"
      },
      {
        "Id": "roles/Admin",
        "Name": "Admin"
      }
    ]
  },
}

我如何进行查询以获取角色名称为“WebUser”的用户列表,但角色名称为“Admin”的用户除外(包含角色“WebUser”但不包含角色“Admin”)

使用LINQ或lucene

2 个答案:

答案 0 :(得分:2)

您需要为此创建索引,例如:

from user in docs.Users
select new { Roles = user.Logins.Roles.Select(x=>x.Name) }

然后你可以查询使用:

Roles:WebMaster AND -Roles:Admin

答案 1 :(得分:1)

这是你想要的吗?

var users = documentSession.Query<User>()
    .Where(x => x.Login.Roles.Any(y => y.Name == "WebUser"))
    .Where(x => x.Login.Roles.Any(y => y.Name != "Admin"))
    .ToList();

样本单元测试....

using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Linq;

namespace Foo.Tests.
{
    public class UserTests
    {
        [Test]
        // ReSharper disable InconsistentNaming
        public void GivenSomeUsersWithWebUserAndAdminRoles_Query_ReturnsSomeUsers()
        // ReSharper restore InconsistentNaming
        {
            IDocumentStore documentStore;
            using (documentStore = new EmbeddableDocumentStore {RunInMemory = true})
            {
                // Arrange.
                documentStore.Initialize();

                // Create and store Fake Data.
                using (IDocumentSession documentSession = documentStore.OpenSession())
                {
                    IEnumerable<User> users = CreateFakeUsers(documentSession);
                    foreach (var user in users)
                    {
                        documentSession.Store(user);
                    }
                    documentSession.SaveChanges();
                }

                using (IDocumentSession documentSession = documentStore.OpenSession())
                {
                    // Act.
                    var users = documentSession.Query<User>()
                        .Where(x => x.Login.Roles.Any(y => y.Name == "WebUser"))
                        .Where(x => x.Login.Roles.Any(y => y.Name != "Admin"))
                        .ToList();

                    // Assert.
                    Assert.IsNotNull(users);
                    Assert.AreEqual(2, users.Count);
                }
            }
        }

        private IEnumerable<User> CreateFakeUsers(IDocumentSession documentSession)
        {
            return new List<User>
                       {
                           new User
                               {
                                   Status = "ACTIVE",
                                   Login = new Login
                                               {
                                                   UserName = "loging",
                                                   Password = null,
                                                   CreationDate = DateTime.UtcNow,
                                                   Roles = new List<Role>
                                                               {
                                                                   new Role
                                                                       {
                                                                           Id = "roles/WebUser",
                                                                           Name = "WebUser"
                                                                       },
                                                                   new Role
                                                                       {
                                                                           Id = "roles/Admin",
                                                                           Name = "Admin"
                                                                       }
                                                               }
                                               }
                               },
                           new User
                               {
                                   Status = "ACTIVE",
                                   Login = new Login
                                               {
                                                   UserName = "User 2",
                                                   Password = null,
                                                   CreationDate = DateTime.UtcNow,
                                                   Roles = new List<Role>
                                                               {
                                                                   new Role
                                                                       {
                                                                           Id = "roles/WebUser",
                                                                           Name = "WebUser"
                                                                       }
                                                               }
                                               }
                               },
                           new User
                               {
                                   Status = "INACTIVE",
                                   Login = new Login
                                               {
                                                   UserName = "User 3",
                                                   Password = null,
                                                   CreationDate = DateTime.UtcNow,
                                                   Roles = new List<Role>
                                                               {
                                                                   new Role
                                                                       {
                                                                           Id = "roles/Admin",
                                                                           Name = "Admin"
                                                                       }
                                                               }
                                               }
                               }
                       };
        }

        #region Nested type: Login

        private class Login
        {
            public string UserName { get; set; }
            public string Password { get; set; }
            public DateTime CreationDate { get; set; }
            public ICollection<Role> Roles { get; set; }
        }

        #endregion

        #region Nested type: Role

        private class Role
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }

        #endregion

        #region Nested type: User

        private class User
        {
            public string Id { get; set; }
            public string Status { get; set; }
            public Login Login { get; set; }
        }

        #endregion
    }
}