MongoDB
|
This is the APOC Extended documentation. APOC Extended is not supported by Neo4j. For the officially supported APOC Core, go to the APOC Core page. |
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 Extended library.
This dependency is included in apoc-mongodb-dependencies-5.11.0-all.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 schememongodb://[username:password@]host1[:port1][,host2[:port2],…[,hostN[:portN]]]/databaseName.collectionName[?options]. Note that this uri must necessarily have the database name and (if thecollectionconfig parameter is not explicit) the collection name (for examplemongodb://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 forapoc.mongo.update) -
documents: the documents to insert (only forapoc.mongo.insert) -
config: see below
Configuration parameters
The procedures support the following config parameters:
| name | type | default | description |
|---|---|---|---|
extractReferences |
|
false |
If true and a field contains an |
objectIdAsMap |
|
true |
If true extract the |
project |
|
empty |
The projection parameters (can be a map or a json string) |
sort |
|
empty |
The sort parameters (can be a map or a json string) |
skip |
|
0 |
The number of documents to skip |
limit |
|
0 |
The max number of documents to show |
collection |
|
empty |
The collection name (takes precedence over the collection passed with |
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.mongo.aggregate('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', [{`$match`: {foo: 'bar'}}, {`$set`: {aggrField: 'Y'} }])
| value |
|---|
|
|
apoc.mongo.count
CALL apoc.mongo.count('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin')
| value |
|---|
3 |
We can also pass the collection name through the config parameter:
CALL apoc.mongo.count('mongodb://user:pass@localhost:27017/myDb?authSource=admin', {collection: 'Person'})
| value |
|---|
3 |
apoc.mongo.find
If we want to extract the all `Person`s with default parameter:
CALL apoc.mongo.find('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin')
| value |
|---|
|
|
|
In addition, we can pass the query param like:
CALL apoc.mongo.first('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', {expr: {`$regex`: 'bar*', `$options`: ''}})
| value |
|---|
|
If we want to extract bought references, through config parameter:
CALL apoc.mongo.first('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', {expr: {`$regex`: 'foo*', `$options`: ''}}, {extractReferences: true})
| value |
|---|
|
Moreover, we can retrieve the ObjectId s with theirs HexString representation through objectIdAsMap config:
CALL apoc.mongo.first('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', {expr: {`$regex`: 'foo*', `$options`: ''}}, {objectIdAsMap: false, extractReferences: true})
| value |
|---|
|
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}})
| value |
|---|
|
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}'})
| value |
|---|
|
|
Furthermore, we can use the limit parameter, for example:
CALL apoc.mongo.find('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', null, {skip: 1, limit: 1, project: {age: 1}})
| value |
|---|
|
Furthermore, we can pass a sort parameter, for example:
CALL apoc.mongo.find('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', null, {sort: {name: -1}, objectIdAsMap: false, project: {name: 1}})
| value |
|---|
`` { "_id": "personJohn", "name": "John", } |
`` { "_id": "personJack", "name": "Jack", } |
|
apoc.mongo.update
To update the age property of the John document:
CALL apoc.mongo.update('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', {name: "John"}, {`$set`: {age:99}})
with the number of row affected as result:
| value |
|---|
1 |
apoc.mongo.delete
To delete the John document:
CALL apoc.mongo.update('mongodb://user:pass@localhost:27017/myDb.Person?authSource=admin', {name: "John"})
with the number of row affected as result:
| 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}])