MongoDB

Available Procedures

type qualified name signature description

procedure

apoc.mongodb.count

apoc.mongodb.count(host :: STRING?, db :: STRING?, collection :: STRING?, query :: MAP?) :: (value :: INTEGER?)

apoc.mongodb.count(host-or-key,db,collection,query) yield value - perform a find operation on mongodb collection

procedure

apoc.mongodb.delete

apoc.mongodb.delete(host :: STRING?, db :: STRING?, collection :: STRING?, query :: MAP?) :: (value :: INTEGER?)

apoc.mongodb.delete(host-or-key,db,collection,query) - delete the given documents from the mongodb collection and returns the number of affected documents

procedure

apoc.mongodb.find

apoc.mongodb.find(host :: STRING?, db :: STRING?, collection :: STRING?, query :: MAP?, project :: MAP?, sort :: MAP?, compatibleValues = false :: BOOLEAN?, skip = 0 :: INTEGER?, limit = 0 :: INTEGER?, extractReferences = false :: BOOLEAN?, objectIdAsMap = true :: BOOLEAN?) :: (value :: MAP?)

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

procedure

apoc.mongodb.first

apoc.mongodb.first(host :: STRING?, db :: STRING?, collection :: STRING?, query :: MAP?, compatibleValues = true :: BOOLEAN?, extractReferences = false :: BOOLEAN?, objectIdAsMap = true :: BOOLEAN?) :: (value :: MAP?)

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

procedure

apoc.mongodb.get

apoc.mongodb.get(host :: STRING?, db :: STRING?, collection :: STRING?, query :: MAP?, compatibleValues = false :: BOOLEAN?, skip = 0 :: INTEGER?, limit = 0 :: INTEGER?, extractReferences = false :: BOOLEAN?, objectIdAsMap = true :: BOOLEAN?) :: (value :: MAP?)

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

procedure

apoc.mongodb.insert

apoc.mongodb.insert(host :: STRING?, db :: STRING?, collection :: STRING?, documents :: LIST? OF MAP?) :: VOID

apoc.mongodb.insert(host-or-key,db,collection,documents) - inserts the given documents into the mongodb collection

procedure

apoc.mongodb.update

apoc.mongodb.update(host :: STRING?, db :: STRING?, collection :: STRING?, query :: MAP?, update :: MAP?) :: (value :: INTEGER?)

apoc.mongodb.update(host-or-key,db,collection,query,update) - updates the given documents from the mongodb collection and returns the number of affected documents

Install Dependencies

The Mongo procedures have dependencies on a client library that is not included in the APOC Library.

This dependency is included in apoc-mongodb-dependencies-4.0.0.18.jar, which can be downloaded from the releases page. Once that file is downloaded, it should be placed in the plugins directory and the Neo4j Server restarted.

Field description

  • hostorkey: the MongoDB host in the format mongodb://<HOST_NAME>:<PORT> or a url defined into the apoc config apoc.mongodb.myInstance.url=mongodb://<HOST_NAME>:<PORT>, which can be invoked by simply passing myInstance

  • db: the db name

  • collection: the collection name

  • query: query params

  • projection: projection params

  • sort: sort params

  • compatibleValues (false|true): converts MongoDB data types into Neo4j data types

  • skip: num of documents to skip

  • limit: num of documents to limit

  • extractReferences (false|true): if true and a field contains an ObjectId it will include the related document instead of the ObjectId

  • objectIdAsMap (true|false): extract the ObjectId as map

  • documents: the documents to insert

  • update: the updated params

Examples

Following an example that could help to understand the behaviour of extractReferences, compatibleValues and objectIdAsMap:

Given the following collections:

// Product
...
{"_id": ObjectId("product1"), "name": "Product 1", "price": 100}
{"_id": ObjectId("product3"), "name": "Product 2", "price": 200}
{"_id": ObjectId("product3"), "name": "Product 3", "price": 300}
...
// Person
...
{"_id": ObjectId("person"), "name": "Andrea", "bought": [ObjectId("product1"), ObjectId("product3")]}
...

With the extractReferences=true, compatibleValues=true and objectIdAsMap=false:

{
  "_id": "person",
  "name": "Andrea",
  "bought": [
    {"_id": "product1", "name": "Product 1", "price": 100},
    {"_id": "product3", "name": "Product 3", "price": 300}
  ]
}

With the extractReferences=true, compatibleValues=true and objectIdAsMap=true:

{
  "_id": {
  	"timestamp": <...>,
	"machineIdentifier": <...>,
	"processIdentifier": <...>,
	"counter": <...>,
  },
  "name": "Andrea",
  "bought": [
    {
      "_id": {
	  	"timestamp": <...>,
		"machineIdentifier": <...>,
		"processIdentifier": <...>,
		"counter": <...>,
	  },
	  "name": "Product 1",
	  "price": 100
	},
    {
      "_id": {
	  	"timestamp": <...>,
		"machineIdentifier": <...>,
		"processIdentifier": <...>,
		"counter": <...>,
	  },
	  "name": "Product 3",
	  "price": 300
	},
  ]
}

With the extractReferences=false, compatibleValues=false and objectIdAsMap=false:

{
  "_id": "person",
  "name": "Andrea",
  "bought": ["product1", "product3"]
}

Dependencies

Copy these jars into the plugins directory:

  • bson-3.4.2.jar

  • mongo-java-driver-3.4.2.jar

  • mongodb-driver-3.4.2.jar

  • mongodb-driver-core-3.4.2.jar

You should be able to get them from here, and here (BSON) (via Download)

Or you get them locally from your gradle build of apoc.

gradle copyRuntimeLibs
cp lib/mongodb*.jar lib/bson*.jar $NEO4J_HOME/plugins/

Example

CALL apoc.mongodb.first('mongodb://localhost:27017','test','test',{name:'testDocument'})

If you need automatic conversion of unpackable values then the cypher query will be:

CALL apoc.mongodb.first('mongodb://localhost:27017','test','test',{name:'testDocument'},true)