我不知道为什么mongodb getCollection不起作用

时间:2019-07-31 07:30:50

标签: java mongodb

我执行Junit Test 1,mongodb不返回结果。它输出调试日志1,不将查找查询发送到mongodb, 但是当我执行Junit Test 1&2时,mongodb会成功插入并找到。打印调试日志2

为什么会发生?我不知道。

代码1:

@Override
public Flowable<Document> fetchAll(int limit, String tableName) {
    MongoCollection<Document> tableColle = mongoDatabase.getCollection(tableName);
    return Flowable.fromPublisher(tableColle.find().limit(limit));
}

@Override
public Flowable<Document> insertAll(List<Document> docs, String tableName) {
    MongoCollection<Document> tableColle = mongoDatabase.getCollection(tableName);
    tableColle.insertMany(docs).subscribe(new Subscriber<Success>() {
        @Override
        public void onSubscribe(Subscription s) {
            s.request(1);

        }
        @Override
        public void onNext(Success t) {
            System.out.println(t.name());
            docs.stream().map(d->d.get("_id"))
            .forEach(System.out::println);
        }
        @Override
        public void onError(Throwable t) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onComplete() {
            // TODO Auto-generated method stub

        }});
    return null;
}

Junit测试1:

@Test
public void fetchAllTest()
{
    Flowable<Document> doc = mongoOperator.fetchAll(10, "autho");
    doc.map(d->d.get("_id"))
    .forEach(System.out::println);
}

Junit测试2:

//   @Test
public void insertAllTest() {
    List<Document> d = new ArrayList<Document>();
    Document d1 = new Document();
    d1.append("test", "value1");
    d1.append("time", new Date());
    Document d2 = new Document();
    d2.append("test", " value2");
    d2.append("time", new Date());
    d.add(d1);
    d.add(d2);
    mongoOperator.insertAll(d, "autho");
}

日志1:

 c.t.mongoOperator.MongoOperatorTests     : Running with Spring Boot v2.1.6.RELEASE, Spring v5.1.8.RELEASE
2019-07-31 15:02:16.790  INFO 14096 --- [           main] c.t.mongoOperator.MongoOperatorTests     : No active profile set, falling back to default profiles: default
2019-07-31 15:02:18.893  INFO 14096 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-07-31 15:02:18.935 DEBUG 14096 --- [           main] org.mongodb.driver.cluster               : Updating cluster description to  {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]
2019-07-31 15:02:19.013  INFO 14096 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:218}] to localhost:27017
2019-07-31 15:02:19.014 DEBUG 14096 --- [localhost:27017] org.mongodb.driver.cluster               : Checking status of localhost:27017
2019-07-31 15:02:19.112 DEBUG 14096 --- [localhost:27017] org.mongodb.driver.protocol.command      : Sending command '{ "ismaster" : 1, "$db" : "admin" }' with request id 4 to database admin on connection [connectionId{localValue:1, serverValue:218}] to server localhost:27017
2019-07-31 15:02:19.117 DEBUG 14096 --- [localhost:27017] org.mongodb.driver.protocol.command      : Execution of command with request id 4 completed successfully in 97.27 ms on connection [connectionId{localValue:1, serverValue:218}] to server localhost:27017
2019-07-31 15:02:19.126  INFO 14096 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 11]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=103257395}
2019-07-31 15:02:19.129 DEBUG 14096 --- [localhost:27017] org.mongodb.driver.cluster               : Updating cluster description to  {type=STANDALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=103.3 ms, state=CONNECTED}]
2019-07-31 15:02:19.177  INFO 14096 --- [           main] c.t.mongoOperator.MongoOperatorTests     : Started MongoOperatorTests in 3.062 seconds (JVM running for 4.694)
2019-07-31 15:02:20.086 DEBUG 14096 --- [localhost:27017] org.mongodb.driver.connection            : Closing connection connectionId{localValue:1, serverValue:218}

日志2:

2019-07-31 15:25:13.129  INFO 948 --- [           main] c.t.mongoOperator.MongoOperatorTests     : Started MongoOperatorTests in 3.366 seconds (JVM running for 4.973)
2019-07-31 15:25:13.841  INFO 948 --- [       Thread-6] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:263}] to localhost:27017
2019-07-31 15:25:13.882 DEBUG 948 --- [       Thread-6] org.mongodb.driver.protocol.command      : Sending command '{ "insert" : "autho", "ordered" : true, "$db" : "autho", "documents" : [{ "_id" : { "$oid" : "5d4142590f192503b4e12010" }, "test" : "value1", "time" : { "$date" : "2019-07-31T07:25:13.742Z" } }, { "_id" : { "$oid" : "5d4142590f192503b4e12011" }, "test" : " value2", "time" : { "$date" : "2019-07-31T07:25:13.742Z" } }] }' with request id 8 to database autho on connection [connectionId{localValue:2, serverValue:263}] to server localhost:27017
2019-07-31 15:25:13.893 DEBUG 948 --- [       Thread-5] org.mongodb.driver.protocol.command      : Execution of command with request id 8 completed successfully in 26.77 ms on connection [connectionId{localValue:2, serverValue:263}] to server localhost:27017
SUCCESS
5d4142590f192503b4e12010
5d4142590f192503b4e12011
2019-07-31 15:25:14.022 DEBUG 948 --- [           main] org.mongodb.driver.protocol.command      : Sending command '{ "find" : "autho", "limit" : 10, "batchSize" : 2147483647, "$db" : "autho", "$readPreference" : { "mode" : "primaryPreferred" } }' with request id 9 to database autho on connection [connectionId{localValue:2, serverValue:263}] to server localhost:27017
2019-07-31 15:25:14.034 DEBUG 948 --- [       Thread-5] org.mongodb.driver.protocol.command      : Execution of command with request id 9 completed successfully in 8.41 ms on connection [connectionId{localValue:2, serverValue:263}] to server localhost:27017
2019-07-31 15:25:14.057 DEBUG 948 --- [localhost:27017] org.mongodb.driver.connection            : Closing connection connectionId{localValue:1, serverValue:262}
2019-07-31 15:25:14.062  INFO 948 --- [       Thread-5] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:2, serverValue:263}] to localhost:27017 because the pool has been closed.
2019-07-31 15:25:14.068 DEBUG 948 --- [       Thread-5] org.mongodb.driver.connection            : Closing connection connectionId{localValue:2, serverValue:263}
5d4128a10f19251fb4619fd3
5d4128a10f19251fb4619fd4
5d4128da0f1925333c57ebff
5d4128da0f1925333c57ec00
5d4129370f192533c46f7248
5d4129370f192533c46f7249
5d412cd20f1925257896dc7a
5d412cd20f1925257896dc7b
5d412f5e0f19250aa0d4b2a7
5d412f5e0f19250aa0d4b2a8

0 个答案:

没有答案