Mongo db并在查询中使用地图

时间:2017-01-12 17:50:23

标签: json mongodb mongo-java-driver

我是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"}

0 个答案:

没有答案
相关问题