如何在mongodb中插入文档

时间:2018-09-07 12:12:13

标签: java mongodb

package com.powerup.email;

import com.mongodb.*;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;


public class SampleRxTestDAO {
  public static void main(String args[]) {
    MongoClientURI uri = new MongoClientURI(
            "mongodb://madmin:madmindev001@cluster0-shard-00-00-lwae7.mongodb.net:27017,cluster0-shard-00-01-lwae7.mongodb.net:27017,cluster0-shard-00-02-lwae7.mongodb.net:27017/powerup?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin");

    MongoClient mongoClient = new MongoClient(uri);
    MongoDatabase database = mongoClient.getDatabase("powerup");
    //System.out.println("Database is successfully connected");
    MongoCollection<Document> testCollection = database.getCollection("ai_email_test001");

    Document myDoc = testCollection.find().first();
    System.out.println(myDoc.toJson());
    Document doc = new Document("fromname", AWSSESEmailSender.fromname)
            .append("from", AWSSESEmailSender.from)
            .append("", "")
            .append("subject", AWSSESEmailSender.subject)
            .append("messageBody", AWSSESEmailSender.messageBody);
    testCollection.insertOne(doc);
    System.out.println("Created" + doc);


  }
}

我想通过使用insert()方法将文档插入到mongodb中,但这在IntelliJ中对我不起作用。我收到此错误:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.apache.james.mime4j.field.MailboxListFieldImpl.

在上面的代码中,我使用insertOne()方法插入文档。

0 个答案:

没有答案