1

I am trying to add a string to the end of an existing array in a mongoDB document.

I have tried looking at the documentation for mongoDB which lead me to the push page and other similar questions. None of them have worked so far as the document that i have have no ids made by me, they are auto-generated as a new element in the array is added.

Document in collection:

_id: 5ce85c1e1c9d4400003dcfd9
name: "Halloween party"
category: 2
date: 2019-10-31T23:00:00.000+00:00
address: "Sample Street, london"
description: "It's Halloween, bring your costumes and your personality to the studen..."
bookings: Array
    0: "1610512"

I am able to get the document that I want to append the string in with the following code.

Java Code:

MongoDatabase database = mongoClient.getDatabase("KioskDB");
MongoCollection<Document> Kiosk = database.getCollection("Events");

Document searchQuery = new Document();
searchQuery.put("name", selectedActivityName);
searchQuery.put("bookings", username);

FindIterable<Document> documents = Kiosk.find(searchQuery);

for (Document document: documents){
     System.out.println(document);
}

Giving me the following output

Document{{_id=5ce85c1e1c9d4400003dcfd9, name=Halloween party, category=2, date=Thu Oct 31 23:00:00 GMT 2019, address=Sample Street, london, description=It's Halloween, bring your costumes and your personality to the student Bar and join us in this age long celebration., bookings=[1610512]}}

How do I go about appending a new string at the end of the array giving me something like this shown below.

Desired final document

_id: 5ce85c1e1c9d4400003dcfd9
name: "Halloween party"
category: 2
date: 2019-10-31T23:00:00.000+00:00
address: "Sample Street, London"
description: "It's Halloween, bring your costumes and your personality to the studen..."
bookings: Array
    0: "1610512"
    1: "1859301"

EDIT: Was able to find the answer with the following code.

DBObject listItem = new BasicDBObject("bookings", username);
Kiosk.updateOne(eq("name", selectedActivityName), new Document().append("$push", listItem));

Where username is the number (Ex: 1859301), selectedActivityName is the name of the name field (Ex: Halloween party) and Kiosk is the collection name.

PawnDelivery
  • 13
  • 1
  • 4

2 Answers2

1

I will try this code according to the documentation. Ref. https://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/

Document doc = new Document("name", "Halloween party")
                    .append("bookings", Arrays.asList("1859301"));
Wing Kui Tsoi
  • 474
  • 1
  • 6
  • 16
0

Since the 3.0 Java driver they added helper methods for filters which make querying mongo a lot nicer and readable. In 3.1 they also added helper methods for updates which make things like this pretty straightforward and easy to understand what is happening See:

https://mongodb.github.io/mongo-java-driver/3.6/javadoc/com/mongodb/client/model/Filters.html https://mongodb.github.io/mongo-java-driver/3.6/javadoc/com/mongodb/client/model/Updates.html

Bson query = Filters.eq("name", selectedActivityName);
Bson update = Updates.push("bookings", username);

collection.findOneAndUpdate(query, update);

Doing this in older versions is possible as well. This syntax should still hold true for pre 3.0 versions. However, if you're running older than 3.0 you'll need to replace Document with BasicDBObject.

Bson query = new Document("name", selectedActivityName);
Bson update = new Document("$push", new Document("bookings", username));

collection.findOneAndUpdate(query, update);
Plancke
  • 1,119
  • 11
  • 12