尝试使用MongoDB Java驱动程序更新文档

编程驴:

谢谢

  • 我只想感谢您点击这个问题!我已尽力使这一过程尽可能的彻底。
  • 但是,如果您需要进一步澄清,请随时告诉我!
  • 如果您认为问题太长。您只需阅读第三部分和第四部分,然后在此处发布您自己的解决方案即可!

建立

  • Mongodb Java驱动程序:org.mongodb:mongo-java-driver:3.11.0-rc0

我想做的事

  • 查找具有特定“名称”字段的特定文档。
  • 然后更新其他字段或整个文档。

范例文件

// the document that I am trying to find in db
{
  "_id":"5de6af7cfa42833bd9849477",
  "name":"Richard Koba",
  "skills":[]
}

// the document that I have
{
  "name":"Richard Koba",
  "skills":[jump, dance, sing]
}

// final result in db
{
  "_id":"5de6af7cfa42833bd9849477",
  "name":"Richard Koba",
  "skills":[jump, dance, sing]
}

我现在在做什么

  // finding a document with same "name" field as input doc and update it with doc
  public MongoCollection updateDocument(Document doc, String colName) {
    MongoCollection collection;

    // make sure collection exist
    try {
      collection = connectCollection(colName); // returns MongoCollection Obj
    } catch (CollectionNotFoundException e) {
      MessageHandler.errorMessage(e.getMessage());
      return null;
    }

    // trying to find the document.
    if (collection.find(eq("name", doc.get("name"))).first() == null) {
      // if document not found, insert a new one
      collection.insertOne(doc);
    } else {
      // if the document found, replace/update it with the one I have
      collection.replaceOne(eq("name", doc.get("name")), doc);
    }

    return collection;
  }

我对错误解决方案的发现

  1. collection.find(eq("name", doc.get("name"))).first() 从不返回null。
  2. Java只告诉我它返回一个对象。MongoDB文档告诉我它是一个TResult,指向MongoIterable<TResult>我被困在这里。
  3. 代码的结果是最后没有插入/更新任何文档。

参考

prasad_:

我尝试了一些代码,这很好。这与您的代码没有太大不同。

mongo shell创建了一个文档

MongoDB Enterprise > db.users.findOne()
{
        "_id" : "5de6af7cfa42833bd9849477",
        "name" : "Richard Koba",
        "skills" : [ ]
}

我的Java代码:

// Test input documents
private Document doc1 = new Document()
                           .append("name", "Richard Koba")
                           .append("skills", Arrays.asList("jump", "dance", "sing"));
private Document doc2 = new Document()
                            .append("name", "Richard K")
                            .append("skills", Arrays.asList("sings"));

doc1传递给以下方法时,结果为:“ ### Doc FOUND”。并且,doc2结果是“找不到文档###”。

private void checkDocument(Document doc) {

    MongoClient mongoClient = MongoClients.create("mongodb://localhost/");
    MongoDatabase database = mongoClient.getDatabase("javadb");
    MongoCollection<Document> collection = database.getCollection("users");

    if (collection.find(eq("name", doc.get("name"))).first() == null) {

        System.out.println("### Doc NOT found");
    } 
    else {
        System.out.println("### Doc FOUND");
    }
}

我也尝试过,结果相同。

Document d = collection.find(eq("name", doc.get("name"))).first();
if (d == null) { // ... }

我也尝试过 也可以。

if (collection.find(queryFilter).iterator().tryNext() == null) { // ... }


我认为您的代码或数据库/集合可能还有其他问题。使用新数据进行一些调试和测试可能会发现真正的问题。

  • 您是否通过mongo shellCompass工具检查了文档中是否已存在该文档
  • 您使用的数据库和集合名称正确吗?
  • 每次测试运行之后,是否要验证数据库中的数据是否已更新/插入?



collection.find(eq(“ name”,doc.get(“ name”)))。first()永远不会返回null。

使用上面发布的代码,nullusers集合为空,find查询确实返回

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

mongodb Java驱动程序3.0:如何存储JSON文档

使用Java驱动程序获取mongoDB中最后插入的文档的ID

Java MongoDB驱动程序:如何更新集合中的所有文档?

使用Java驱动程序在MongoDB中更新阵列

如何在Java mongodb驱动程序中使用“ _id”字段查询文档?

更新MongoDB Java驱动程序中的多行

如何使用Java驱动程序更新mongo db中的文档字段?

使用Java驱动程序方法在MongoDB子文档上检索ObjectID

MongoDB使用Java驱动程序版本3.0更新

使用Java 3驱动程序更新MongoDB

MongoDB C#驱动程序2.0-更新文档

如何使用C#驱动程序更新MongoDB数组中的子文档

Mongodb:如何使用mongodb Java驱动程序更改嵌套文档的值

MongoDB Java驱动程序使用$ in过滤器创建文档

使用驱动程序忽略_id的文档更新

如何使用MongoDB的Java驱动程序将许多文档合并为一个文档?

带有聚合管道的MongoDB C#驱动程序更新文档

如何使用.NET的MongoDB驱动程序的updateOne()更新而不替换文档

无法使用php驱动程序删除mongodb中的文档

如何使用C#驱动程序更新MongoDB中数组子文档中包含的数组子文档中的字段?

MongoDb无法在分片环境中使用Java驱动程序插入文档

我们如何使用MongoDB Java驱动程序在MongoDB中追加/更新集合的子文档?

使用C#MongoDB驱动程序尝试使用FindAll

MongoDB:使用Java驱动程序拉

加载链接文档MongoDB驱动程序

mongoDB-光标仅使用node.js驱动程序更新1个文档

如何使用 Java 驱动程序在 MongoDB 中获取嵌入文档数组的特定值

使用 .Net 驱动程序异步更新或插入 MongoDB 文档

无法使用 NodeJS MongoDB 驱动程序更新文档