简体   繁体   English

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

[英]querying couchbase with spring-data-couchbase, using multiple columns

I am using couchbase3 with spring-data-couchbase , and want to query data using spring data repository with multiple columns.我将 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
}

How should I write Map function and Reduce function for the same?我应该如何编写 Map 函数和 Reduce 函数?

For the function findByEmail(Query eMail);对于函数findByEmail(Query eMail); to work, I have added the view with Map fn()为了工作,我添加了带有 Map fn() 的视图

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

This view have email as key, and value is the document.此视图以电子邮件为键,值为文档。 But if i need to query using email and status?但是如果我需要使用电子邮件和状态查询? How should the view look like ?视图应该是什么样子的?

I have seen this link, but not very clear.我看过这个链接,但不是很清楚。 https://stackoverflow.com/questions/28938755 https://stackoverflow.com/questions/28938755

您可以在此处的文档中使用像描述一样的复合键: http : //docs.couchbase.com/developer/dev-guide-3.0/compound-keys.html

I was able make springdata function to invoke a compound Key view.我能够使 springdata 函数调用复合键视图。 My Document name is : 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 : SpringData Repository 接口如下所示:

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

    }
}

If this was useful for you, please up vote如果这对您有用,请投票

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring-data-couchbase-运行更新查询 - Spring-data-couchbase - running update query spring-data-couchbase中的@Query与Pageable参数 - @Query with Pageable parameter in spring-data-couchbase 如何使用 spring-data-couchbase 为特定的 Couchbase 文档设置 TTL? - How to set TTL for a specific Couchbase document using spring-data-couchbase? spring-data-couchbase对不存在的文档抛出DocumentDoesNotExistException - spring-data-couchbase throws DocumentDoesNotExistException for non-existent documents 在spring-data-couchbase(N1QL)中计数查询 - Counting query in spring-data-couchbase (N1QL) 使用PageRequest查找所有文档时出现spring-data-couchbase错误 - spring-data-couchbase error while finding all documents with PageRequest 我正在使用spring-data-couchbase,但是从方法名称创建查询不起作用 - I am using spring-data-couchbase , but the Query creation from method names does not work 如何使用带有@Query 的 spring-data-couchbase 从文档中返回 boolean - How to return a boolean from a document using spring-data-couchbase with @Query spring-data-couchbase - org.springframework.data.mapping.model.MappingException - spring-data-couchbase - org.springframework.data.mapping.model.MappingException Spring-Data-Couchbase-运行非临时参数化查询 - Spring-data-couchbase - running non ad-hoc parametrized query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM