I have a Java example program that I continually hack on to learn. My example may have bits that don't apply, but the basis for the question in here. Notice use of "$push"...
This assumes a record is available to query and push a new array item...
package test.barry;
public class Main {
public static void main(String[] args) {
com.mongodb.client.MongoDatabase db = connectToClusterStandAlone();
InsertArrayItem(db);
return;
}
private static void InsertArrayItem(com.mongodb.client.MongoDatabase db) {
System.out.println("");
System.out.println("Starting InsertArrayItem...");
com.mongodb.client.MongoCollection<org.bson.Document> collection = db.getCollection("people");
com.mongodb.client.MongoCursor<org.bson.Document> cursor = collection.find(com.mongodb.client.model.Filters.eq("testfield", true)).sort(new org.bson.Document("review_date", -1)).limit(1).iterator();
if(cursor.hasNext()) {
org.bson.Document document = cursor.next();
Object id = document.get("_id");
System.out.println("Selected Id: " + id.toString());
org.bson.Document newDocument = new org.bson.Document("somekey", "somevalue");
collection.findOneAndUpdate(
com.mongodb.client.model.Filters.eq("_id", id),
new org.bson.Document("$push", new org.bson.Document("myarray", newDocument))
);
}
System.out.println("Completed InsertArrayItem.");
}
private static com.mongodb.client.MongoDatabase connectToClusterStandAlone() {
// STANDALONE STILL REQUIRES HOSTS LIST WITH ONE ELEMENT...
// http://mongodb.github.io/mongo-java-driver/3.9/javadoc/com/mongodb/MongoClientSettings.Builder.html
java.util.ArrayList<com.mongodb.ServerAddress> hosts = new java.util.ArrayList<com.mongodb.ServerAddress>();
hosts.add(new com.mongodb.ServerAddress("127.0.0.1", 27017));
com.mongodb.MongoCredential mongoCredential = com.mongodb.MongoCredential.createScramSha1Credential("testuser", "admin", "mysecret".toCharArray());
com.mongodb.MongoClientSettings mongoClientSettings = com.mongodb.MongoClientSettings.builder()
.applyToClusterSettings(clusterSettingsBuilder -> clusterSettingsBuilder.hosts(hosts))
.credential(mongoCredential)
.writeConcern(com.mongodb.WriteConcern.W1)
.readConcern(com.mongodb.ReadConcern.MAJORITY)
.readPreference(com.mongodb.ReadPreference.nearest())
.retryWrites(true)
.build();
com.mongodb.client.MongoClient client = com.mongodb.client.MongoClients.create(mongoClientSettings);
com.mongodb.client.MongoDatabase db = client.getDatabase("test");
return db;
}
}
Example document after running twice...
{
"_id" : ObjectId("5de7f472b0ba4011a7caa59c"),
"name" : "someone somebody",
"age" : 22,
"state" : "WA",
"phone" : "(739) 543-2109",
"ssn" : "444-22-9999",
"testfield" : true,
"versions" : [
"v1.2",
"v1.3",
"v1.4"
],
"info" : {
"x" : 444,
"y" : "yes"
},
"somefield" : "d21ee185-b6f6-4b58-896a-79424d163626",
"myarray" : [
{
"somekey" : "somevalue"
},
{
"somekey" : "somevalue"
}
]
}
For completeness here is my maven file...
Maven POM File...
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.barry</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputDirectory>${basedir}</outputDirectory>
<finalName>Test</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>test.barry.Main</mainClass>
</transformer>
</transformers>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.10.1</version>
</dependency>
</dependencies>
</project>