带有 APOC 和 MongoDB 驱动程序的 Neo4J,限制来自 Mongo 的返回记录

Neo4J with APOC and MongoDB Driver, limiting returned records from Mongo

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

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

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

是否有办法对流前查询结果添加限制?

目前这不是开箱即用的。但是你可以添加这个功能。

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