MongoDB

Available Procedures

signature

apoc.mongo.aggregate(uri, pipeline, $config) yield value - perform an aggregate operation on mongodb collection

apoc.mongo.count(uri, query, $config) yield value - perform a count operation on mongodb collection

apoc.mongo.find(uri, query, $config) yield value - perform a find operation on mongodb collection

apoc.mongo.delete(uri, query, $config) - delete the given documents from the mongodb collection and returns the number of affected documents

apoc.mongo.insert(uri, documents, $config) yield value - inserts the given documents into the mongodb collection

apoc.mongo.update(uri, query, update, $config) - 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.3.0.12.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.

Alternatively, you could 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 the following links:

Field description

  • uri: The connection String URI, with scheme mongodb://[username:password@]host1[:port1][,host2[:port2],…​[,hostN[:portN]]]/databaseName.collectionName[?options]. Note that this uri must necessarily have the database name and (if the collection config parameter is not explicit) the collection name (for example mongodb://user:pass@localhost:27017/myDb.myCollection?authSource=admin)

  • query: query parameter map (can be a map or a json string)

  • update: update parameter map (only for apoc.mongo.update)

  • documents: the documents to insert (only for apoc.mongo.insert)

  • config: see below

Configuration parameters

The procedures support the following config parameters:

Table 1. Config parameters
name type default description

extractReferences

Boolean

false

If true and a field contains an ObjectId it will include the related document instead of the ObjectId

objectIdAsMap

Boolean

true

If true extract the ObjectId as map

project

Map<K,V> OR String

empty

The projection parameters (can be a map or a json string)

sort

Map<K,V> OR String

empty

The sort parameters (can be a map or a json string)

skip

Long

0

The number of documents to skip

limit

Long

0

The max number of documents to show

collection

String

empty

The collection name (takes precedence over the collection passed with uri parameter

Examples

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("personAl"), "name": "Al", expr: BsonRegularExpression("foo*"), "bought": [ObjectId("product1"), ObjectId("product3")]}
{"_id": ObjectId("personJohn"), "name": "John", "age": 40, "foo", "bar"}
{"_id": ObjectId("personJack"), "name": "Jack", "age": 50, "foo", "bar", expr: BsonRegularExpression("bar*"), "bought": [ObjectId("product1"), ObjectId("product2")]}
...

we can run the following procedures.

apoc.mongo.aggregate

CALL apoc.mongodb.aggregate('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', [{`$match`: {foo: 'bar'}}, {`$set`: {aggrField: 'Y'} }])
Table 2. Results
value

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "name": "John", "foo": "bar", "age": 40L, "aggrField": "Y", }

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "name": "Jack", "age": 50L, "foo": "bar", "expr": "bar*", "bought": ["product1", "product2"], "aggrField": "Y", }

apoc.mongo.count

CALL apoc.mongodb.count('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin')
Table 3. Results
value

3

We can also pass the collection name through the config parameter:

CALL apoc.mongodb.count('mongodb://user:pass@localhost:27017/myDb?authSource=admin', {collection: 'Person'})
Table 4. Results
value

3

apoc.mongo.find

If we want to extract the all `Person`s with default parameter:

CALL apoc.mongodb.find('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin')
Table 5. Results
value

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "name": "Al", "expr": "foo*", "bought": ["product1", "product3"] }

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "name": "John", "foo": "bar", "age": 40L }

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "name": "Jack", "age": 50L, "foo": "bar", "expr": "bar*", "bought": ["product1", "product2"] }

In addition, we can pass the query param like:

CALL apoc.mongodb.first('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', {expr: {`$regex`: 'bar*', `$options`: ''}})
Table 6. Results
value

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "name": "Jack", "foo": "bar", "expr": "bar*", "bought": ["product1", "product2"] }

If we want to extract bought references, through config parameter:

CALL apoc.mongodb.first('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', {expr: {`$regex`: 'foo*', `$options`: ''}}, {extractReferences: true})
Table 7. Results
value

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "name": "Al", "expr": "foo*", "bought": [ { "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "name": "Product 1", "price": 100 }, { "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "name": "Product 3", "price": 300 }, ] }

Moreover, we can retrieve the ObjectId s with theirs HexString representation through objectIdAsMap config:

CALL apoc.mongodb.first('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', {expr: {`$regex`: 'foo*', `$options`: ''}}, {objectIdAsMap: false, extractReferences: true})
Table 8. Results
value

{ "_id": "personAl", "name": "Al", "expr": "foo*", "bought": [ {"_id": "product1", "name": "Product 1", "price": 100}, {"_id": "product3", "name": "Product 3", "price": 300} ] }

Furthermore, we can skip n values and pass a project parameter:

CALL apoc.mongo.first('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', null, {skip: 2, project: {age: 1}})
Table 9. Results
value

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "age": 50L, }

We can pass query, skip and sort parameter as stringified values, for example:

CALL apoc.mongo.first('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', '{foo: "bar"}', {sort: '{name: -1}', project: '{age: 1}'})
Table 10. Results
value

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "age": 40L, }

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "age": 50L, }

Furthermore, we can use the limit parameter, for example:

CALL apoc.mongodb.find('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', null, {skip: 1, limit: 1, project: {age: 1}})
Table 11. Results
value

{ "_id": { "timestamp": <…​>, "machineIdentifier": <…​>, "processIdentifier": <…​>, "counter": <…​>, }, "age": 40, }

Furthermore, we can pass a sort parameter, for example:

CALL apoc.mongodb.find('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', null, {sort: {name: -1}, objectIdAsMap: false, project: {name: 1}})
Table 12. Results
value

`` { "_id": "personJohn", "name": "John", }

`` { "_id": "personJack", "name": "Jack", }

{ "_id": "personAl", "name": "Al", }

apoc.mongo.update

To update the age property of the John document:

CALL apoc.mongodb.update('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', {name: "John"}, {`$set`: {age:99}})

with the number of row affected as result:

Table 13. Results
value

1

apoc.mongo.delete

To delete the John document:

CALL apoc.mongodb.update('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', {name: "John"})

with the number of row affected as result:

Table 14. Results
value

1

apoc.mongo.insert

To insert 2 document {"secondId": ObjectId("507f191e811c19729de860ea"), "baz": 1} and {"secondId": ObjectId("507f191e821c19729de860ef"), "baz": 1} in a Person collection (in this case the procedure return void):

CALL apoc.mongo.insert('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', [{secondId: {`$oid`: '507f191e811c19729de860ea'}, baz: 1}, {secondId: {`$oid`: '507f191e821c19729de860ef'}, baz: 1}])