【问题标题】:Neo4J with APOC and MongoDB Driver, limiting returned records from MongoNeo4J 与 APOC 和 MongoDB 驱动程序,限制从 Mongo 返回的记录
【发布时间】:2017-11-23 18:56:42
【问题描述】:

在 MongoDB 中限制返回记录的数量很简单 db.collection.find().limit(n)。 但是我想从 Neo4J 发出等效的查询。

鉴于从 Neo4J 发出的查找查询如下... apoc.mongodb.find(host, db, collection, query, project, sort) 我发现很难看出应该如何告诉 MongoDB 实例在流式传输到 Neo4J 之前限制返回的结果。

我知道 Cypher 的 LIMIT 子句,但是,考虑到将从 Mongo 流式传输的冗余数据量,这感觉像是一种不好的做法。

还有没有办法为查询结果预流添加限制?

【问题讨论】:

    标签: java mongodb neo4j cypher neo4j-apoc


    【解决方案1】:

    目前还没有开箱即用的功能。但是您可以添加此功能。

    APOC source code 中进行以下更改:

    neo4j-apoc-procedures/src/main/java/apoc/mongodb/MongoDB.java:


    @Procedure
    @Description("apoc.mongodb.find(host-or-port,db-or-null,collection-or-null,query-or-null,projection-or-null,sort-or-null,[compatibleValues=true|false]) yield value - perform a find,project,sort operation on mongodb collection")
    public Stream<MapResult> find(@Name("host") String hostOrKey, @Name("db") String db, @Name("collection") String collection, @Name("query") Map<String, Object> query, @Name("project") Map<String, Object> project, @Name("sort") Map<String, Object> sort, @Name(value = "compatibleValues", defaultValue = "false") boolean compatibleValues) {
        return getMongoColl(hostOrKey, db, collection, compatibleValues).find(query, project, sort).map(MapResult::new);
    }
    

    interface Coll extends Closeable {
    
    ...
    
        Stream<Map<String, Object>> find(Map<String, Object> query, Map<String, Object> project, Map<String, Object> sort, Map<String, Object> pagination);
    

    neo4j-apoc-procedures/src/main/java/apoc/mongodb/MongoDBColl.java:


    @Override
    public Stream<Map<String, Object>> find(Map<String, Object> query, Map<String, Object> project, Map<String, Object> sort, Map<String, Object> pagination) {
        FindIterable<Document> documents = query == null ? collection.find() : collection.find(new Document(query));
        if (project != null) documents = documents.projection(new Document(project));
        if (sort != null) documents = documents.sort(new Document(sort));
        if (pagination != null) {
            Object skip = pagination.get("skip");
            Object limit = pagination.get("limit");
            if (skip != null) documents = documents.skip(Integer.parseInt(String.valueOf(skip)));
            if (limit != null) documents = documents.limit(Integer.parseInt(String.valueOf(limit)));
        }
        return asStream(documents);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      相关资源
      最近更新 更多