-2

This is my current code

Document bitcoin = new Document("Currency", "Bitcoin").append("Values", Arrays.asList());
//bitCoinCollection.insertOne(bitcoin);

for(int i = 0; i<100;i++){
    BasicDBObject setNewFieldQuery = new BasicDBObject()
                    .append("$set", new BasicDBObject().append("value", 100));
    mongoClient.getDatabase("Binance").getCollection("Binance")
               .updateOne(new BasicDBObject().append("_id", "tjena"), setNewFieldQuery);
}

It doesnt add any values to the excisting arraylist... is there a way to add it to an excisting arraylist in a document? Thanks in advance

1

1 Answer 1

0

For this sample collection of one document with an array field using MongoDB Java driver:

{ _id: 1, fruits: [ "orange", "banana" ] }

The following Java code adds one new element to the fruits array:

Bson update = push("fruits", "guava");
Bson filter = eq("_id", new Integer(1));
UpdateResult result = collection.updateOne(filter, update);

To add multiple elements all at once from a List collection:

Bson update = pushEach("fruits", Arrays.asList("grape", "apple", "peach"));
Bson filter = eq("_id", new Integer(1));
UpdateResult result = collection.updateOne(filter, update);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.