0

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

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

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

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

4

1 回答 1

1

目前,这不是开箱即用的。但是您可以添加此功能。

APOC 源代码中进行以下更改:

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);
}
于 2017-11-24T23:52:55.390 回答