Does MongoDB Support Recursive Queries With Java API?

41 views
Skip to first unread message

Nick Apperley

unread,
Oct 19, 2016, 10:55:12 PM10/19/16
to mongodb-user
Trying to do a basic query in MongoDB using the Java API. Always end up with no results being returned. Below is the sample Kotlin code (image version):


package org.example.mongodbtest

import com.mongodb.BasicDBObject
import com.mongodb.MongoClient
import com.mongodb.client.MongoCollection
import com.mongodb.client.model.Filters.gt
import org.bson.Document

object DBTest {
@JvmStatic
fun main(args: Array<String>) {
val dbClient = MongoClient()
val db = dbClient.getDatabase("test")
val collection = db.getCollection("test")
val myDoc = collection.find(gt("x", 200)).first()

//insertDocument(collection)
//clearCollection(collection)
println("Collection Count: ${collection.count()}")
collection.find(BasicDBObject("x", 203)).forEach { println("Find Item: ${it.toJson()}") }
//println("First Document: ${if (myDoc?.toJson() != null) myDoc.toJson() else "No Document found"}")
}

fun clearCollection(collection: MongoCollection<Document>) {
collection.deleteMany(Document())
}

fun insertDocument(collection: MongoCollection<Document>) {
val doc = Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("info", Document("x", 203).append("y", 102))

collection.insertOne(doc)
}
}



Here is the text version:

package org.example.mongodbtest

import com.mongodb.BasicDBObject
import com.mongodb.MongoClient
import com.mongodb.client.MongoCollection
import org.bson.Document

object DBTest {
    @JvmStatic
    fun main(args: Array<String>) {
        val dbClient = MongoClient()
        val db = dbClient.getDatabase("test")
        val collection = db.getCollection("test")
        val myDoc = collection.find(gt("x", 200)).first()

        //insertDocument(collection)
        //clearCollection(collection)
        println("Collection Count: ${collection.count()}")
        collection.find(BasicDBObject("x", 203)).forEach { println("Find Item: ${it.toJson()}") }
        //println("First Document: ${if (myDoc?.toJson() != null) myDoc.toJson() else "No Document found"}")
    }

    fun clearCollection(collection: MongoCollection<Document>) {
        collection.deleteMany(Document())
    }

    fun insertDocument(collection: MongoCollection<Document>) {
        val doc = Document("name", "MongoDB")
                .append("type", "database")
                .append("count", 1)
                .append("info", Document("x", 203).append("y", 102))

        collection.insertOne(doc)
    }
}


Does MongoDB support recursive queries with the Java API?

Wan Bachtiar

unread,
Nov 1, 2016, 2:29:02 AM11/1/16
to mongodb-user

Does MongoDB support recursive queries with the Java API?

Hi Nick,

If you are intending to query for the document that you inserted from insertDocument(), where {info: {x: 230}} . What you are looking for is querying embedded document.

You can do this in MongoDB Java Driver either:

MongoCursor<Document> cursor = collection.find(eq("info.x", 203)).iterator();

Or, using Document class:

MongoCursor<Document> cursor = collection.find(new Document("info.x", 203)).iterator();

Note that the current stable version of the MongoDB Java driver is v3.3.

If the above doesn’t solve your issue, could you:

  • Elaborate what you are trying to do with example input and desired output.
  • Provide your MongoDB Java driver version.

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages