简体   繁体   English

如何正确保存到mongodb?

[英]How to save properly to mongodb?

So this is my first time using MongoDB and I'm having an issue with saving into a document所以这是我第一次使用 MongoDB,我在保存到文档时遇到了问题

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)获取护照的方法(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.您的get函数似乎只返回没有其他识别数据的护照数组,然后getPassport函数只返回来自匹配国家/地区的护照对象。

This means that at the time you call这意味着在你打电话的时候

mongoCollection.updateOne(document, ... )

The contents of document will look something like: document的内容将类似于:

        {
            "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.这将只匹配来自 mongoCollection 的具有顶级字段countryissuedBycitydate和带有显示的确切值的stamps文档。 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.另请注意, updateOne适用于文档,而不是子数组的元素,因此替换stamps可能不会像您想要的那样工作。

You might be able to use array filters, or the pipeline-form of update, with $map or $filter to edit just that matching element.您可以使用数组过滤器或更新的管道形式,通过 $map 或 $filter 来编辑匹配的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM