apoc.mongodb.get.byObjectId

Procedure APOC Full

apoc.mongodb.get.byObjectId(hostOrKey, db, collection, objectIdValue, config(default:{})) - get the document by Object id value

Signature

apoc.mongodb.get.byObjectId(host :: STRING?, db :: STRING?, collection :: STRING?, objectIdValue :: STRING?, config = {} :: MAP?) :: (value :: MAP?)

Input parameters

Name Type Default

host

STRING?

null

db

STRING?

null

collection

STRING?

null

objectIdValue

STRING?

null

config

MAP?

{}

Config parameters

The procedure supports the following config parameters:

Table 1. Config parameters
name type default description

compatibleValues

boolean

true

converts MongoDB data types into Neo4j data types

extractReferences

boolean

false

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

objectIdAsMap

boolean

true

extract the _id as map

idFieldName

String

_id

the field name of the ObjectId

Output parameters

Name Type

value

MAP?

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.1.0.11.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.

Usage Examples

Field description

  • host: 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

  • objectIdValue: the ObjectId of the document to retrieve

  • config: the config map

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}
{"_id": ObjectId("product4"), "name": ObjectId("507f191e810c19729de860ea"), "price": 400}
...
// Person
...
{"_id": ObjectId("person"), "name": "Andrea", "bought": [ObjectId("product1"), ObjectId("product3")]}
...

With CALL apoc.mongodb.get.byObjectId(<HOST>, <DB>, "product", "product1"):

{
  "_id": {
  	"timestamp": <...>,
	"machineIdentifier": <...>,
	"processIdentifier": <...>,
	"counter": <...>,
  }
  "name": "Product 1",
  "price": 100L
}

With CALL apoc.mongodb.get.byObjectId(<HOST>, <DB>, "product", "product4", {idFieldName: "name"}):

{
  "_id": {
  	"timestamp": <...>,
	"machineIdentifier": <...>,
	"processIdentifier": <...>,
	"counter": <...>,
  }
  "name": "507f191e810c19729de860ea",
  "price": 400L
}

With CALL apoc.mongodb.get.byObjectId(<HOST>, <DB>, "product", "product1", "_id", {extractReferences: true, objectIdAsMap: true, compatibleValues: false}):

{
  "_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
	},
  ]
}