Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为mongoDB连接创建一个方法,并在Java中用不同的方法编写查询?_Java_Mongodb_Methods - Fatal编程技术网

如何为mongoDB连接创建一个方法,并在Java中用不同的方法编写查询?

如何为mongoDB连接创建一个方法,并在Java中用不同的方法编写查询?,java,mongodb,methods,Java,Mongodb,Methods,我已经为MongoDB连接创建了一个方法,可以打印集合中的所有文档 public static void connectMongowithURI_1() { String db_name = "YYYY", db_col_name = "XXXX"; String client_url = "mongodb:localhost XXXXX"; MongoClientURI uri = new MongoClientURI(client_

我已经为MongoDB连接创建了一个方法,可以打印集合中的所有文档

    public static void connectMongowithURI_1() {

        String db_name = "YYYY", db_col_name = "XXXX";

        String client_url = "mongodb:localhost XXXXX";
        MongoClientURI uri = new MongoClientURI(client_url);

        // Connecting to the mongodb server using the given client uri.
        MongoClient mongo_client = new MongoClient(uri);

        // Fetching the database from the mongodb.
        MongoDatabase db = mongo_client.getDatabase(db_name);

        // Fetching the collection from the mongodb.
        MongoCollection<Document> coll = db.getCollection(db_col_name);
        //log.info("Fetching all documents from the collection");
        System.out.println("Fetching all documents from the collection");

        BasicDBObject whereQuery = new BasicDBObject();
        whereQuery.put("SyncAccount.DataArea.GeneralInfo.AccountBusinessKey",101871);

        //FindIterable<Document> cursor = coll.find(whereQuery);
        // Performing a read operation on the collection.
        FindIterable<Document> fi = coll.find(whereQuery);
        MongoCursor<Document> cursor = fi.iterator();

        try {
            while(cursor.hasNext()) {
                //log.info(cursor.next().toJson());
                System.out.println(cursor.next().toJson());
            }
        } finally {
            cursor.close();
        }
    }
此方法用于写查询。但这是行不通的。谁能帮我解决这个问题

public static void findAllDocument(String dataBaseName, String collectionName) {

    MongoDatabase db = connectMongowithURI().getDatabase(dataBaseName);
    System.out.println("Connected_1");
    MongoCollection<Document> collection = db.getCollection(collectionName);
    System.out.println("Connected_2");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("SyncAccount.DataArea.GeneralInfo.AccountBusinessKey",101871);
    System.out.println("Connected_3");

    //FindIterable<Document> cursor = coll.find(whereQuery);
    // Performing a read operation on the collection.
    FindIterable<Document> fi = collection.find(whereQuery);
    System.out.println("Connected_5");

    fi.noCursorTimeout(true);    // ERROR MESSAGE GENERATE AFTER THIS LINE.
    MongoCursor<Document> cursor = fi.iterator();
    System.out.println("Connected_6");

    try {
        while(cursor.hasNext()) {
            //log.info(cursor.next().toJson());
            System.out.println(cursor.next().toJson());
        }`enter code here`
    } finally {
        cursor.close();
    }
}

当您说它不起作用时,请共享异常和确切原因以进一步帮助您。线程“main”com.mongodb.MongoQueryException中的异常:查询失败,错误代码为13,错误消息“not authorized catch the exception at at the“
enter code here
”位于try块。在网上寻找关于如何使用“try catch finally”的例子。在
catch
块中执行
printStackTrace()
-这将获得详细的异常消息。用消息更新帖子。方法
connectMongowithURI()
中的
mongoURI
值与
connectMongowithURI\u 1()
客户端url
中的值不同。也可以看到这篇带有类似错误的文章:。因此,错误本身解释了由于未授权请求而拒绝连接。
public static void findAllDocument(String dataBaseName, String collectionName) {

    MongoDatabase db = connectMongowithURI().getDatabase(dataBaseName);
    System.out.println("Connected_1");
    MongoCollection<Document> collection = db.getCollection(collectionName);
    System.out.println("Connected_2");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("SyncAccount.DataArea.GeneralInfo.AccountBusinessKey",101871);
    System.out.println("Connected_3");

    //FindIterable<Document> cursor = coll.find(whereQuery);
    // Performing a read operation on the collection.
    FindIterable<Document> fi = collection.find(whereQuery);
    System.out.println("Connected_5");

    fi.noCursorTimeout(true);    // ERROR MESSAGE GENERATE AFTER THIS LINE.
    MongoCursor<Document> cursor = fi.iterator();
    System.out.println("Connected_6");

    try {
        while(cursor.hasNext()) {
            //log.info(cursor.next().toJson());
            System.out.println(cursor.next().toJson());
        }`enter code here`
    } finally {
        cursor.close();
    }
}
Exception in thread "main" com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorize. at com.mongodb.operation.FindOperation$1.call(FindOperation.java:735)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:725)
at com.mongodb.operation.OperationHelper.withReadConnectionSource(OperationHelper.java:463)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:725)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:89)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:189)
at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:143)
at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92)
at dB.connection.ConnectToMongoDB.findAllDocument(ConnectToMongoDB.java:80)
at dB.connection.ConnectToMongoDB.main(ConnectToMongoDB.java:21)