
apoc.mongodb.MongoDB Maven / Gradle / Ivy
package apoc.mongodb;
import apoc.Extended;
import apoc.result.LongResult;
import apoc.result.MapResult;
import apoc.util.MissingDependencyException;
import apoc.util.UrlResolver;
import org.bson.types.ObjectId;
import org.neo4j.logging.Log;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.Procedure;
import java.io.Closeable;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
/*
also see https://docs.mongodb.com/ecosystem/tools/http-interfaces/#simple-rest-api
// Load courses from MongoDB
CALL apoc.load.json('http://127.0.0.1:28017/course_catalog/course/') YIELD value
UNWIND value.rows AS course
// Create Course nodes
MERGE (c:Course {_id: course._id['$oid']})
ON CREATE SET c.name = course.name
WITH course, c
// Create Category nodes and connect to Course
FOREACH (category IN course.categories |
MERGE (cat:Category {_id: category.id})
ON CREATE SET cat.name = category.name
MERGE (c)-[:HAS_CATEGORY]->(cat)
)
WITH course, c
// Create University nodes and connect to Course
UNWIND course.universities AS univ
WITH c, univ WHERE univ.id IS NOT NULL
MERGE (u:University {id: univ.id})
ON CREATE SET u.location = univ.location,
u.shortName = univ.shortName,
u.url = univ.website
MERGE (c)-[:OFFERED_BY]->(u)
*/
@Extended
public class MongoDB {
@Context
public Log log;
@Deprecated
@Procedure
@Description("apoc.mongodb.get(host-or-key,db,collection,query,[compatibleValues=false|true],skip-or-null,limit-or-null,[extractReferences=false|true],[objectIdAsMap=true|false]) yield value - perform a find operation on mongodb collection")
public Stream get(@Name("host") String hostOrKey,
@Name("db") String db,
@Name("collection") String collection,
@Name("query") Map query,
@Name(value = "compatibleValues", defaultValue = "false") boolean compatibleValues,
@Name(value = "skip", defaultValue = "0") Long skip,
@Name(value = "limit", defaultValue = "0") Long limit,
@Name(value = "extractReferences", defaultValue = "false") boolean extractReferences,
@Name(value = "objectIdAsMap", defaultValue = "true") boolean objectIdAsMap) {
return executeMongoQuery(hostOrKey, db, collection, compatibleValues,
extractReferences, objectIdAsMap, coll -> coll.all(query, skip, limit).map(MapResult::new),
e -> log.error("apoc.mongodb.get - hostOrKey = [" + hostOrKey + "], db = [" + db + "], collection = [" + collection + "], query = [" + query + "], compatibleValues = [" + compatibleValues + "], skip = [" + skip + "], limit = [" + limit + "]", e));
}
@Deprecated
@Procedure
@Description("apoc.mongodb.count(host-or-key,db,collection,query) yield value - perform a find operation on mongodb collection")
public Stream count(@Name("host") String hostOrKey, @Name("db") String db, @Name("collection") String collection, @Name("query") Map query) {
return executeMongoQuery(hostOrKey, db, collection, false,
false,
false, coll -> {
long count = coll.count(query);
return Stream.of(new LongResult(count));
},
e -> log.error("apoc.mongodb.count - hostOrKey = [" + hostOrKey + "], db = [" + db + "], collection = [" + collection + "], query = [" + query + "]",e));
}
private Coll getColl(@Name("host") String hostOrKey, @Name("db") String db, @Name("collection") String collection,
boolean compatibleValues, boolean extractReferences, boolean objectIdAsMap) {
String url = getMongoDBUrl(hostOrKey);
return Coll.Factory.create(url, db, collection, compatibleValues, extractReferences, objectIdAsMap);
}
@Deprecated
@Procedure
@Description("apoc.mongodb.first(host-or-key,db,collection,query,[compatibleValues=false|true],[extractReferences=false|true],[objectIdAsMap=true|false]) yield value - perform a first operation on mongodb collection")
public Stream first(@Name("host") String hostOrKey, @Name("db") String db, @Name("collection") String collection, @Name("query") Map query, @Name(value = "compatibleValues", defaultValue = "true") boolean compatibleValues,
@Name(value = "extractReferences", defaultValue = "false") boolean extractReferences,
@Name(value = "objectIdAsMap", defaultValue = "true") boolean objectIdAsMap) {
return executeMongoQuery(hostOrKey, db, collection, compatibleValues,
extractReferences,
objectIdAsMap, coll -> {
Map result = coll.first(query);
return result == null || result.isEmpty() ? Stream.empty() : Stream.of(new MapResult(result));
},
e -> log.error("apoc.mongodb.first - hostOrKey = [" + hostOrKey + "], db = [" + db + "], collection = [" + collection + "], query = [" + query + "], compatibleValues = [" + compatibleValues + "]",e));
}
@Deprecated
@Procedure
@Description("apoc.mongodb.find(host-or-key,db,collection,query,projection,sort,[compatibleValues=false|true],skip-or-null,limit-or-null,[extractReferences=false|true],[objectIdAsMap=true|false]) yield value - perform a find,project,sort operation on mongodb collection")
public Stream find(@Name("host") String hostOrKey,
@Name("db") String db,
@Name("collection") String collection,
@Name("query") Map query,
@Name("project") Map project,
@Name("sort") Map sort,
@Name(value = "compatibleValues", defaultValue = "false") boolean compatibleValues,
@Name(value = "skip", defaultValue = "0") Long skip,
@Name(value = "limit", defaultValue = "0") Long limit,
@Name(value = "extractReferences", defaultValue = "false") boolean extractReferences,
@Name(value = "objectIdAsMap", defaultValue = "true") boolean objectIdAsMap) {
return executeMongoQuery(hostOrKey, db, collection, compatibleValues,
extractReferences, objectIdAsMap, coll -> coll.find(query, project, sort, skip, limit).map(MapResult::new),
e -> log.error("apoc.mongodb.find - hostOrKey = [" + hostOrKey + "], db = [" + db + "], collection = [" + collection + "], query = [" + query + "], project = [" + project + "], sort = [" + sort + "], compatibleValues = [" + compatibleValues + "], skip = [" + skip + "], limit = [" + limit + "]",e));
}
@Deprecated
@Procedure
@Description("apoc.mongodb.insert(host-or-key,db,collection,documents) - inserts the given documents into the mongodb collection")
public void insert(@Name("host") String hostOrKey, @Name("db") String db, @Name("collection") String collection, @Name("documents") List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy