0

我是 mongo 的新手,我正在尝试找出查询,我正在使用 java 驱动程序

我在数据库中有一个文件如下

{"name":"joe", "surname":"bloggs", "other": {"name":"fred", "surname"    :   "flint"}, "important" : "important info that does not chnage" }

现在我想对文档做一个 upsert 并且只在它不存在时更新它

我有以下内容,如果它不存在我想插入它,如果它存在则更新。

        Bson filter = Filters.and(
                Filters.eq("name", oldObj.getName()),
                Filters.eq("surname", oldObj.getSurname()),
                Filters.eq("translations", oldObj.getOtherMap())
        );



    Document documentToInsertUpdate = new Document();
    documentToInsertUpdate.put("important", newObj.getInfo());
    documentToInsertUpdate.put("name", newObj.getName());
    documentToInsertUpdate.put("surname", newObj.getSurname());
    documentToInsertUpdate.put("other", newObj.getOtherMap());

    Bson update = new Document("$set", documentToInsertUpdate);
    UpdateOptions options = new UpdateOptions().upsert(true);

collection.updateOne(filter, update, options,   getSingleResultCallback(new ArrayList<Translations>()));

当我尝试在过滤器和文档中设置 json 的“其他”部分时似乎有问题,它不起作用。如果我从过滤器和 documentToInsertUpdate 中删除“其他”部分,它工作正常。

我如何处理第二部分,即

"other": {"name":"fred", "surname" : "flint"}
4

0 回答 0