Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

MongoDB join collections

farenda 2017-04-14 0

For long time MongoDB didn’t have any means to join collections, but since version 3.2 there is a way to do simple left outer join of two collections.

MongoDB collections to join

Let’s say that we’ve got the following two MongoDB collections on which we would like to perform JOIN (like in SQL).

db.highscores.find()
{ "_id" : 1, "name" : "QES", "score" : 600 }
{ "_id" : 2, "name" : "FPG", "score" : 214 }
{ "_id" : 3, "name" : "RHS", "score" : 453 }

db.players.find()
{ "_id" : 5, "name" : "QES", "email" : "qes@example.com" }
{ "_id" : 6, "name" : "FPG", "email" : "fpg@example.com" }
{ "_id" : 7, "name" : "RHS", "email" : "rhs@example.com" }

In our case we’ll use the name field as the join field.

MongoDB join collections

Since MongoDB we can use $lookup aggregation operator to perform left outer join on a single field. The syntax is simple and boils down to providing:

  • collection to join using from field,
  • localField, which should be used in equality match,
  • foreignField, to indicate the field from the other collection,
  • what should be the name of joined documents inside the result.

Let’s see an example. We are looking for a player with the name “QES” and want to join his data from the players collection:

db.highscores.aggregate([
 {$match: {name: "QES"}},
 {$lookup: {
   from: "players",
   localField: "name",
   foreignField: "name",
   as: "player"
 }}
])

The above aggregation pipeline joins highscores and players collections and produces the following document:

{
  "_id" : 1,
  "name" : "QES",
  "score" : 600,
  "player" : [
    { "_id" : 5, "name" : "QES", "email" : "qes@example.com" }
  ]
}

Notice that the joined data from players collection is inside an array. This is because more than one document may match. When there would be no matching documents, the array would be empty.

References:

  • MongoDB Manual
  • MongoDB Tutorial
Share with the World!
Categories MongoDB Tags mongodb
Previous: MongoDB JSON to Document/DBObject
Next: MD5 hash in Java

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok