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

时间:2015-06-29 13:13:06

标签: java couchbase couchbase-view spring-data-couchbase

我正在使用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(查询电子邮件); 工作,我添加了视图与地图fn()

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

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

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

2 个答案:

答案 0 :(得分:1)

您可以在此处的文档中使用复合键,例如:http://docs.couchbase.com/developer/dev-guide-3.0/compound-keys.html

答案 1 :(得分:1)

我能够使用springdata函数来调用复合键视图。 我的文档名称是:数据 复合关键视图

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

SpringData Repository Inferface如下所示:

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);
}

测试类如下所示:

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));

    }
}

如果这对您有用,请投票