mongo-java-driver

Mongo Java Driver 3.6.1 - How to parse a json array structured String to BasicDBList

假如想象 提交于 2019-12-11 13:32:40
问题 When I use MongoDB 2.x.x I used (BasicDBList) JSON.parse("[]") to parse the string data to Document array. But the latest MongoDB driver says this is deprecated and the BasicDbObject.parse("") only converts to BasicDBObject . Here is some code samples I earlier used in 2.x.x Java driver BasicDbList skuList = (BasicDBList) JSON.parse(skus); So when I upgraded to 3.6.1, the compiler says this is deprecated. And suggested to use BasicDbObject.parse() But this only accepts JSON objects structured

Can't connect MongoDb via JNDI in java

我的未来我决定 提交于 2019-12-11 11:57:43
问题 I am trying to connect MongoDB through JNDI in wildfly using the below code. Context ctx = new InitialContext(); MongoClient mongoClient = (MongoClient) ctx.lookup("java:global/MyMongoClient"); But i am getting the following error. Exception->com.mongodb.MongoClient cannot be cast to com.mongodb.MongoClient I am using mongo-java-driver-3.4.0.jar in "wildfly-10.1.0.Final/modules/system/layers/base/org/mongodb/main" directory and "mongodb-driver-3.4.0.jar" in "wildfly-10.1.0.Final/standalone

Updating nested array document in MongoDB

瘦欲@ 提交于 2019-12-11 07:59:17
问题 So I have a mongo document like this and i need to update the array based on the val { "_id" : NumberLong(222), "pattern":"grain" "BASIC" : { "frame":"clear" "tin" : [ { "val" : "abc", "unit" : NumberLong(2311) }, { "val" : "def", "unit" : NumberLong(2311) }, ] } } here is the code i've tried collection = db.getCollection("test"); Bson where = new Document().append("_id", 222).append("BASIC.tin.val","abc"); Bson update = new Document() .append("BASIC.tin.$.val", "xyz"); Bson set = new

Can't run Grails app with mongodb plugin version 3.0.3

坚强是说给别人听的谎言 提交于 2019-12-11 03:59:05
问题 I'm trying to upgrade my mongodb plugin from 2.0.1 to 3.0.3. However I keep getting the following error: java.lang.NoClassDefFoundError: com/mongodb/AggregationOptions at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2531) at java.lang.Class.getDeclaredMethods(Class.java:1855) at org.codehaus.groovy.reflection.CachedClass$3$1.run(CachedClass.java:84) at java.security.AccessController.doPrivileged(Native Method) at org.codehaus

How can I authenticate any database with given username and password in Mongo Java Driver 2.13.0?

天涯浪子 提交于 2019-12-11 02:23:38
问题 Previously I could use db.authenticate(String username, char[] password) method. With 2.13.0, how can I achieve this? 回答1: There is no replacement for db.authenticate(). The driver will use the credentials provided and make sure the connections are authenticated as they are created. Based on this mongodb-user discussion the Java Driver team is open to discussions on what the real need for the db.authenticate(...) method. 回答2: Use import com.mongodb.MongoCredential; MongoCredential mongoCred =

java code gives error in mongodb while compiling

╄→гoц情女王★ 提交于 2019-12-05 18:25:41
I am new to mongodb and i have the following code import com.mongodb.*; import com.mongodb.Block; import com.mongodb.client.AggregateIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import org.bson.types.ObjectId; import static java.util.Arrays.asList; public class getAssets{ public static void main( String args[] ){ Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document.toJson()); } }; MongoClient mongoClient = new MongoClient("localhost"

MongoDB BasicDBObject vs Document in java

此生再无相见时 提交于 2019-12-04 06:41:44
I am using MongoDB v3.2.0 with Mongo Java Driver 3.0.4 version. I am using the BasicDBObject (deprecated) instead of using the Document in java, as I need to do many changes for converting to Document in my standalone java project. Can anyone tell me changing into Document, will there be any performance improvement in memory and large collection inserts and reads. Is there any way to improve my frequent write and read operations on MongoDB using java. Basic DBobject is not deprecated . The only deprecated part in the BasicDBobject is DBPointer class and few other methods like toString() and

How do I catch a MongoSecurityException?

时光怂恿深爱的人放手 提交于 2019-12-02 20:51:23
问题 I'm trying to validate the login details of a specific user here. This just won't work. I have no idea why, it never reaches the catch block even though there is a MongoSecurityException . Does someone know why? try{ MongoCredential credential = MongoCredential.createCredential("user", "admin", "password".toCharArray()); ServerAddress address = new ServerAddress("localhost", 27017); mongoClient = new MongoClient(address, Arrays.asList(credential)); }catch (MongoSecurityException e) { e

How do I catch a MongoSecurityException?

断了今生、忘了曾经 提交于 2019-12-02 07:53:01
I'm trying to validate the login details of a specific user here. This just won't work. I have no idea why, it never reaches the catch block even though there is a MongoSecurityException . Does someone know why? try{ MongoCredential credential = MongoCredential.createCredential("user", "admin", "password".toCharArray()); ServerAddress address = new ServerAddress("localhost", 27017); mongoClient = new MongoClient(address, Arrays.asList(credential)); }catch (MongoSecurityException e) { e.printStackTrace(); } Update: Stack trace: May 24, 2017 1:01:08 AM com.mongodb.diagnostics.logging.JULLogger

How to execute queries with both AND and OR clauses in MongoDB with Java

杀马特。学长 韩版系。学妹 提交于 2019-12-02 00:16:29
问题 I want to execute query in MongoDB 3.2 with Java Driver 3.2, which contains both $and and $or clauses at the same time. With the reference, I tried the following approach: List<Document> criteria1 = new ArrayList<>(); List<Document> criteria2 = new ArrayList<>(); criteria1.add(new Document("fetchStatus", new Document("$gte", FetchStatus.PROCESSED_NLP.getID()))); criteria1.add(new Document("fetchStatus", new Document("$lte", fetchStatusParam))); criteria1.add(new Document("episodeID", new