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 3 array query operations

farenda 2016-09-21 0

MongoDB 3 array query operations are very helpful and simplify digging in nested structures. In this post we’ll show how to create queries that match array elements in various ways.

Test collection with book authors

Let’s use the following collection with authors stored in arrays:

db.books.find().pretty()
{ "_id" : 1, "authors" : [ "Joyce", "Dante" ] }
{ "_id" : 2, "authors" : [ "Tolstoy", "Joyce" ] }
{ "_id" : 3, "authors" : [ "Dante", "Joyce" ] }

Documents with only these authors in the same order (the same array)

Console version

db.books.find({authors: ["Joyce", "Dante"]})

{ "_id" : 1, "authors" : [ "Joyce", "Dante" ] }

Java version

// this code is uncool, but works:
BsonArray values = new BsonArray();
values.add(new BsonString("Joyce"));
values.add(new BsonString("Dante"));
books.find(Filters.eq("authors", values));

Any document with Joyce among authors

Console version

db.books.find({authors: "Joyce"})

{ "_id" : 1, "authors" : [ "Joyce", "Dante" ] }
{ "_id" : 2, "authors" : [ "Tolstoy", "Joyce" ] }
{ "_id" : 3, "authors" : [ "Dante", "Joyce" ] }

Java version

books.find(Filters.eq("authors", "Joyce"))

Any document with these two authors in any order in the array

Console version

db.books.find({authors: {$all: ["Joyce", "Dante"]}})

{ "_id" : 1, "authors" : [ "Joyce", "Dante" ] }
{ "_id" : 3, "authors" : [ "Dante", "Joyce" ] }

Java version

// all(field name, value1, value2, ...)
books.find(Filters.all("authors", "Joyce", "Dante"));

Find on specific position in an array:

Console version

db.books.find({"authors.0": "Joyce"}) // 1
{ "_id" : 1, "authors" : [ "Joyce", "Dante" ] }

db.books.find({"authors.1": "Joyce"}) // 2, 3
{ "_id" : 2, "authors" : [ "Tolstoy", "Joyce" ] }
{ "_id" : 3, "authors" : [ "Dante", "Joyce" ] }

Java version

books.find(Filters.eq("authors.0", "Joyce"));
books.find(Filters.eq("authors.1", "Joyce"));
Share with the World!
Categories MongoDB Tags java, mongodb
Previous: Towers of Hanoi recursive version
Next: MongoDB 3 replica set configuration

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