Hot questions for Using Azure in mongodb

Question:

I have employee collection with 140 documents

{

    "name" : "name1",

    "dept" : "tech",

    "address" : "adr3"

}

In all 140 documents dept is tech When I execute the query in azure mongodb

    MongoDatabase db = mongo.getDatabase("test")
    def query = new BasicDBObject(['dept':['$in':['tech']]])
    FindIterable documents = db.getCollection("employees").find(query)
    def outList =  documents.collect {it}

I get only 101 documents instead of 140 records

This works fine in local mongodb. Till last week this was working in azure for me. Am I doing something wrong here ? Anyone facing similar situation ?

If I change query to

def query = new BasicDBObject(['dept':'tech'])

I get all 140 documents

If I change batch size to 10, $in query returns only 10 documents.


Answer:

Xal. According to the sharing in this case : Cosmos Mongo API "In" Array expression issue ,it seems that the issue on Microsoft side.You could wait until the bug fixed.

The reply as below:

Thank you David for reporting this! I investigated the issue, it’s a bug on our side manifesting under a combination of conditions. I already have made a fix for it and will check it in by end of week (then it’s up to our deployment cycle to propagate the fix to all datacenters around the world). Let me know if you have queries that don’t work and are blocking you. Best regards, Orestis

Hope it helps you.

Question:

After 30 mins of inactivity or so I start getting the below error when I try to insert into mongo, When I try again it starts to work. Error Below. I'm on Azure:

[INFO ] 2018-09-10T12:00:43,188 [http-nio-8080-exec-6] connection - Closed connection [connectionId{localValue:3, serverValue:26}] to XX.XX.XX.XX:27017 because there was a socket exception raised by this connection.
[ERROR] 2018-09-10T12:00:43,189 [http-nio-8080-exec-6] [dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Timeout while receiving message; nested exception is com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message] with root cause
java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method) ~[?:1.8.0_181]
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[?:1.8.0_181]
        ava.net.SocketTimeoutException: Read timed out
t java.net.SocketInputStream.read(SocketInputStream.java:171) ~[?:1.8.0_181]
        at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[?:1.8.0_181]

Here is how I initialize my mongo Template:

@Bean
    public MongoDbFactory mongoDbFactory() {
        String[] addresses = mongoUri.split(",");
        List<ServerAddress> servers = new ArrayList<>();
        for (String address : addresses) {
            String[] split = address.trim().split(":");
            servers.add(new ServerAddress(split[0].trim(), Integer.parseInt(split[1].trim())));
        }
        MongoClientOptions.Builder mongoOperations = MongoClientOptions.builder();
        mongoOperations.socketTimeout(1000 * 20); // I tried to increase the socket timeout to see if it helps but no help either
        mongoOperations.connectTimeout(1000 * 10);
        MongoClient mongoClient = new MongoClient(servers, MongoCredential.createCredential(userName, dbName, password.toCharArray()), mongoOperations.build());
        return new SimpleMongoDbFactory(mongoClient, dbName);
    }

    @Bean
    public MongoTemplate getMongoTemplate() {
        return new MongoTemplate(mongoDbFactory());
    }

My mongod version is 3.6.4 and I'm using the same version of java driver. I tried to increase/decrease the tcp_keepalive_time setting as provided in the docs using sudo sysctl -w net.ipv4.tcp_keepalive_time=120 but no help either.


Answer:

ohk. so we found that the mongo java driver jar was older than the mongo server we were using which was causing this issue. make sure that the driver supports the version of mongo server.

Question:

Am trying to put a document on my MongoDB located in Azure. The problem I am facing is that I can't connect to the service. Azure provides the connector string so it should be working.

I am getting this errors

03-29 15:47:24.265 3578-3578/name.bagi.levente.pedometer W/org.bson.ObjectId: Failed to get process identifier from JMX, using random number instead                                                                      
        java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
    Caused by: java.lang.ClassNotFoundException: Didn't find class "java.lang.management.ManagementFactory"

and

FATAL EXCEPTION: main                                                                           Process: name.bagi.levente.pedometer, PID: 3578                                                                           java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.security.CodeSource java.security.ProtectionDomain.getCodeSource()' on a null object reference

I am using this code to connect

//Line below causes exception
MongoClient mongoClient = new MongoClient(new MongoClientURI("AzureProvidedString?ssl=true&sslInvalidHostNameAllowed=true")); 
        BasicDBObject document = new BasicDBObject();
        document.put("id",2);
        document.put("location", 3);
        document.put("steps", 4);
        mongoClient.getDatabase("FitnessData").getCollection("FitnessCollection").insertOne(Document.parse(document.toJson()));

My dependencies

dependencies{
        compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
        // https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver
        compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.2'

    }

I tried using Mongo driver 3.2.2 and am getting a different error. Other solutions here seem to be the same and are working.


Answer:

It sounds like you want to access MongoDB on Azure from Android using Mongo Driver for Java. There was a similar SO thread mongodb 3.x driver Android compatibility which got a same issue with yours. The issue reason is the offical driver mongo-java-driver which is incompatible on Android. A solution is using the third-party driver porting for Android https://github.com/matfur92/mongo-java-driver.

However, directly access MongoDB on Android is not a good choice, even for sending data. Per my experience, the best practice is use REST webservice for sending & storing data on MongoDB, please refer to the MongoDB reference HTTP Interface to get fully featured RESTful Web Services powered by MongoDB on Azure WebApps, or just create Azure Mobile App to receive data from Android.