appending data to array in MongoDB-Document using Java Driver 3.4 - java

I am using MongoDB Java Driver 3.4 and want to update a document (with id "12") in a Mongo-DB collection. Currently, the document looks as follows:
{"id" : "12", "Data" : [{"Author" : "J.K. Rowling",
"Books" : {"Harry Potter 1" : "$15.99", "Harry Potter 2" : "$16.49", "Harry Potter 3" : "$19.49"}},
{"Author" : "Philip Roth",
"Books" : {"American Pastoral" : "$12.99", "The Human Stain" : "$39.49", "Indignation" : "$29.49"}}
]}
I want to update this document by adding the following object to array "Data":
{"Author" : "Stephen King", "Books" : {"Rose Red" : "$12.69", "Faithful" : "$9.49", "Throttle" : "$8.49"}}
To this end I wrote the following java code:
Document doc = new Document().append("Author", "Stephen King")
.append("Data", new Document("Rose Red", "$12.69")
.append("Faithful" : "$9.49")
.append("Throttle" : "$8.49"));
collection.update({"id", "12"}, {$push: {"Data" : doc.toJson()}});
The IDE indicates there is something wrong with the last statement (collection.update...) by showing me this:
I don't know what this error message is supposed to tell me. There is a semicolon at the end of the statement. Does anybody know what's wrong with the java code?
P.S.: This is not a duplicate to
(MongoDB Java) $push into array
My question relates to Java Driver 3.4. The other question relates to an earlier version with totally different classes.

There are couple of things incorrect here.
field - Change from Data to Books
separator - Change from semicolon to comma
method - Change from update to updateOne and you can pass Document directly.
Document doc = new Document("Author", "Stephen King")
.append("Books", new Document("Rose Red", "$12.69").append("Faithful", "$9.49").append("Throttle", "$8.49"));
collection.updateOne(new Document("id", "12"), Updates.push("Data", doc));

Related

MongoDb Java Driver toJson() and $oid

I'm building a Java Jersey API which uses MongoDb and MongoDb driver.
The resources should output JSON of the stored MongoDb document to be used in the frontend project using Svelte.
Due to the standard org.bson.Document.toJson() implementation the output of my documents look somehow like:
[{ "_id" : { "$oid" : "5e97f08f2175aa9174dbec0e" }, "hour" : 8, "minute" : 15, "enabled" : true, "duration" : 120 }
I would rather like it to be:
[{ "_id" : "5e97f08f2175aa9174dbec0e", "hour" : 8, "minute" : 15, "enabled" : true, "duration" : 120 }
That way it's easier to handle the id in the frontend. So how to get rid of the $oid object?
I already managed to get the format as I wish by using:
JsonWriterSettings settings = JsonWriterSettings.builder()
.outputMode(JsonMode.RELAXED)
.objectIdConverter((value, writer) -> writer.writeString(value.toHexString()))
.build();
System.out.println(doc.toJson(settings));
But how to register this setting object globally so that every doc.toJson() call will use it?
And what will happen if I send modified or new documents from the frontend to the API and do:
Document document = Document.parse(doc);
Is my modified _id field automatically converted again to an ObjectId? Or do I need a org.bson.codecs.Decoder or CodecRegistry? How would this be done?
$oid refers to ObjectId field type in bson spec. As far as I know, you need to manipulate your document to replace ObjectId for your _id into String.
String oidAsString = document.getObjectId("_id").toString();
document.put("_id", oidAsString);

mongodb java driver pullByFilter

I have document schema such as
{
"_id" : 18,
"name" : "Verdell Sowinski",
"scores" : [
{
"type" : "exam",
"score" : 62.12870233109035
},
{
"type" : "quiz",
"score" : 84.74586220889356
},
{
"type" : "homework",
"score" : 81.58947824932574
},
{
"type" : "homework",
"score" : 69.09840625499065
}
]
}
I have a solution using pull that copes with removing a single element at a time but saw
I want to get a general solution that would cope with irregular schema where there would be between one and many elements to the array and I would like to remove all elements based on a condition.
I'm using mongodb driver 3.2.2 and saw this pullByFilter which sounded good
Creates an update that removes from an array all elements that match the given filter.
I tried this
Bson filter = and(eq("type", "homework"), lt("score", highest));
Bson u = Updates.pullByFilter(filter);
UpdateResult ur = collection.updateOne(studentDoc, u);
Unsurprisingly, this did not have any effect since I wasn't specifying the array scores
I get an error
The positional operator did not find the match needed from the query. Unexpanded update: scores.$.type
when I change the filter to be
Bson filter = and(eq("scores.$.type", "homework"), lt("scores.$.score", highest));
Is there a one step solution to this problem?
There seems very little info on this particular method I can find. This question may relate to How to Update Multiple Array Elements in mongodb
After some more "thinking" (and a little trial and error), I found the correct Filters method to wrap my basic filter. I think I was focusing on array operators too much.
I'll not post it here in case of flaming.
Clue: think "matches..." (as in regex pattern matching) when dealing with Filters helper methods ;)

Tell if a BasicMongoDBObject is valid from the .toString()?

I'd like to confirm that a parser I wrote is working correctly. It takes a JavaScript mongodb command that could be run from the terminal and converts it to a Java object for the MongoDB/Java drivers.
Is the following .toString() result valid?
{ "NumShares " : 1 , "attr4 " : 1 , "symbol" : { "$regex" : "ESLR%"}}
This was converted from the following JavaScript
db.STOCK.find({ "symbol": "ESLR%" }, { "NumShares" : 1, "attr4" : 1 })
And of course, the data as it rests in the collections
{ "_id" : { "$oid" : "538c99e41f12e5a479269ed1"} , "symbol" : "ESLR" , "NumShares" : 3471.0}
Thanks for all your help
You've combined the query document and the project document in that find() call in to one document. That's probably not what you want. But those documents are just json so you could use any parser to convert those. There's a few gotchas you'd have to deal with around ObjectIDs, dates, DBRefs, and particularly regular expressions but those can be managed without too much trouble by escaping/quoting them before parsing.

MongoDB find with regex behaves differently from Java

Regex find with MongoDB 2.4.6 is not behaving the same way as the Java Pattern class does. Can anyone explain why?
Inserting data in MongoDB:
db.Project.insert({ "_id" : "e0b57d9e-744c-471e-ae95-22a389d2988d", "name" : "Project.20131106101344433" });
Finding all Projects:
db.Project.find()
{
"_id" : "e0b57d9e-744c-471e-ae95-22a389d2988d",
"name" : "Project.20131106101344433"
}
Finding all Projects whose name is "t":
db.Project.find({"name" : /t/})
{
"_id" : "e0b57d9e-744c-471e-ae95-22a389d2988d",
"name" : "Project.20131106101344433"
}
Checking that sole Project name does not match regex "t":
#Test
public void regex() {
assertTrue(!Pattern.matches("t", "Project.20131106101344433"));
}
As you see, the regex db.Project.find returns a Project whose name is not "t", but does contain "t". What am I missing?
Thanks!
In this case db.Project.find({"name" : /t/}) you are not looking for a document whose name is t, you are looking for every document whose name contains t. You can read about PECL here and test what are you doing here.
To find exact match you have to do {"name" : 't'}

Modify nest document's value in MongoDB for Java

A very quick question, how am I going to do this below:
> db.blog.posts.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"title" : "A Blog Post",
"content" : "...",
"author" : {
"name" : "joe",
"email" : "joe#example.com"
}
}
I saw the answer in Javascript is like:
> db.blog.posts.update({"author.name" : "joe"}, {"$set" : {"author.name" : "joe schmoe"}})
But how am I going to do that in Java?
If I have a very deep level value has to be changed, am I supposed to use this way? like: "person.abc.xyz.name.address" ?
Using dot notation to access nested documents will work perfectly well in the Java Driver. Take a look at this StackOverflow answer:
MongoDB nested documents searching
For the Java Driver, the basic idea is to replace the Javascript objects with instances of BasicDBObject.
Here's another good reference for updating:
MongoDb's $set equivalent in its java Driver

Categories