apoc.mongodb.update

Procedure APOC Full Deprecated

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

Signature

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

Input parameters

Name Type Default

host

STRING?

null

db

STRING?

null

collection

STRING?

null

query

MAP?

null

update

MAP?

null

Output parameters

Name Type

value

INTEGER?

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:

Usage Examples

The examples in this section are based on a Mongo DB instance with a pre-populate twitter dataset. You can find instructions for setting this up at github.com/neo4j-examples/mongo-example.

We’re going to update the retweet_count for the document with an id of 591999465512382500. We can check its current value, by running the following query using apoc.mongodb.find.

Cypher
CALL apoc.mongodb.find('mongodb://mongo:neo4j@mongo:27017', 'test', 'tweets', {id: 591999465512382500.0}, null, null)
YIELD value
RETURN value._id, value.text, value.retweet_count
LIMIT 1;
Table 1. Results
value._id value.text value.retweet_count

"553bbecae8f1e57878b72a1e"

"[CALENDAR] Barça have 5 league games left, 2 #UCL semi-final games, and the Spanish Cup final: http://t.co/mWKOzNEWFo http://t.co/cyN1ZZNsSx"

95.0

We can update that field, by running the following query:

Cypher
CALL apoc.mongodb.update('mongodb://mongo:neo4j@mongo:27017', 'test', 'tweets', {id: 591999465512382500.0}, {`$set`:{retweet_count:96.0}});
Table 2. Results
value

1

And now let’s query it again:

Cypher
CALL apoc.mongodb.find('mongodb://mongo:neo4j@mongo:27017', 'test', 'tweets', {id: 591999465512382500.0}, null, null)
YIELD value
RETURN value._id, value.text, value.retweet_count
LIMIT 1;
Table 3. Results
value._id value.text value.retweet_count

"553bbecae8f1e57878b72a1e"

"[CALENDAR] Barça have 5 league games left, 2 #UCL semi-final games, and the Spanish Cup final: http://t.co/mWKOzNEWFo http://t.co/cyN1ZZNsSx"

96.0