首页 文章

使用spring-data-couchbase查询couchbase,使用多列

提问于
浏览
2

我正在使用couchbase3和 spring-data-couchbase ,并希望使用具有多列的spring数据库来查询数据 .

public interface UserAccountRepository extends CrudRepository<UserAccount, Long> {
public UserAccount findByEmail(Query eMail);
public UserAccount findByEmailAndStatus(Query query); // fn with multiple column, but not getting the result
}

我应该如何编写Map函数和Reduce函数呢?

为了使函数 findByEmail(Query eMail); 起作用,我添加了带有Map fn()的视图

function (doc, meta) {
  emit(doc.email,doc);
}

此视图将电子邮件作为密钥,值为文档 . 但是,如果我需要使用电子邮件和状态查询?视图应该如何?

我看过这个链接,但不是很清楚 . https://stackoverflow.com/questions/28938755

2 回答

  • 1

    你可以在这里的文档中使用复合键,如describe:http://docs.couchbase.com/developer/dev-guide-3.0/compound-keys.html

  • 1

    我能够使springdata函数调用复合Key视图 . 我的文档名称是:Data Compound Key View

    function (doc, meta) {
      if(doc && doc._class == "com.couchbase.entity.Data"){
        emit([doc.key1, doc.key2], doc);
      }
    }
    

    SpringData Repository Inferface shown below :

    package com.couchbase.repository;
    
    import java.util.List;
    import org.springframework.data.couchbase.core.view.View;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.data.repository.query.Param;
    import com.couchbase.client.protocol.views.Query;
    import com.couchbase.entity.Data;
    
    public interface DataRepository extends CrudRepository<Data, String> {
    
        @View(designDocument="Data",viewName="findByKey1AndKey2")
        public List<Data> findByKey1AndKey2(Query query);
    }
    

    Test Class shown below :

    import com.couchbase.client.protocol.views.ComplexKey;
    import com.couchbase.client.protocol.views.Query;
    
    public class DataTest extends WebAppConfigurationAware{
    
        @Autowired
        private DataRepository dataRepository;
    
        @Test
        public void testStringDataCompoundQuery(){
            Object[] objArr = new Object[2];
            objArr[0] = "aaa";
            objArr[1] = 1;
    
            Query query = new Query();
            query.setKey(ComplexKey.of(objArr));
    
            System.out.println(dataRepository.findByKey1AndKey2(query));
    
        }
    }
    

    如果这对你有用,请投票

相关问题