So this is my first time using MongoDB and I'm having an issue with saving into a document
Saving method
public void addStamp(UUID playerUUID, UUID issuedByUUID, String playerCountry, String enteringCountry, String enteringCity, boolean isEntry, Date date) {
Document document = getPassport(playerUUID, playerCountry);
DBObject stamp = new BasicDBObject("stampFor", enteringCountry)
.append("cityEntered", enteringCity)
.append("issuedBy", issuedByUUID)
.append("isEntry", isEntry)
.append("date", date);
List<DBObject> stamps = (List<DBObject>) document.get("stamps");
stamps.add(stamp);
mongoCollection.updateOne(document, new Document("$set", new Document("stamps", stamps)));
}
Method that gets the passport (getPassport)
public Document getPassport(UUID uuid, String country) {
List<Document> list;
if (get(uuid, "passports") != null) list = get(uuid, "passports");
else return null;
for (Document document : list) {
if (document.getString("country").equalsIgnoreCase(country))
return document;
}
return null;
}
The get method
public <T> T get(UUID uuid, String key) {
Document document = mongoCollection.find(new Document("UUID", uuid)).first();
if (document == null) return null;
if (document.get(key) == null) return null;
return (T) document.get(key);
}
How the document is structured,
{
"_id": {
"$oid": "5e71c1ea12ee5455287a968f"
},
"UUID": {
"$uuid": "12642edb-f181-4fbf-bfff-642097f1cc69"
},
"passports": [
{
"country": "usa",
"issuedBy": {
"$uuid": "12642edb-f181-4fbf-bfff-642097f1cc69"
},
"city": "nyc",
"date": {
"$date": "2020-03-18T06:38:34.734Z"
},
"stamps": []
}
]
}
What I'm trying to do is edit the stamps part of the document, but with the code I have, it executes with no issues but it does no changes to the documents, I'm not sure what the issue is.
Your get
function appears to return only the passports array with no other identifying data, the getPassport
function then returns only the passport object from the matching country.
This means that at the time you call
mongoCollection.updateOne(document, ... )
The contents of document
will look something like:
{
"country": "usa",
"issuedBy": {
"$uuid": "12642edb-f181-4fbf-bfff-642097f1cc69"
},
"city": "nyc",
"date": {
"$date": "2020-03-18T06:38:34.734Z"
},
"stamps": []
}
This will only match documents from the mongoCollection that have the top-level fields country
, issuedBy
, city
, date
, and stamps
with the exact values shown. Since this is not what the documents in that collection look like, it doesn't match anything, which is not an error, but it also means that nothing gets updated.
Also note that updateOne
applies to a document, not an element of a sub-array, so replacing stamps
is probably not going to work out like you want.
You might be able to use array filters, or the pipeline-form of update, with $map or $filter to edit just that matching element.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.