mongodb-java

MongoDB java driver how to add arrays whith mixed data types

这一生的挚爱 提交于 2019-12-11 07:54:51
问题 I am trouble because I want to increment a date object in 1 hour, with the java driver , this is: {"tDate":{$add: ["$tDate", 3600*1000]} making, wont work because mongoDB expects a number and receives a string String [] date_add_array = {"$t_tDate", String.valueOf(3600*1000) }; BasicDBObject query_component = new BasicDBObject("tDate", new BasicDBObject("$add", date_add_array)) >exception: $add only supports numeric or date types, not String Using a BasicDBList object list won't work, because

Java code for mongodb aggregation query with $in

让人想犯罪 __ 提交于 2019-12-11 06:18:27
问题 I am new to mongodb and need an equivalent code in java for the below db.asset.aggregate([{ $unwind : '$asset' }, { $match : { 'asset.status' : { $in : ['1', '2', '3'] }, 'asset.siteid' : { $in : ['123'] } } } ]).pretty() I tried the following but it didn't help-out DBObject unwind = new BasicDBObject("$unwind", "$dp.asset"); DBObject match = BasicDBObjectBuilder.start().push("$match") .push("dp.asset.status").add("$in", ['ACTIVE', 'LIMITEDUSE', 'OPERATING']) .push("dp.asset.siteid").add("$in

Check if there is an error in update/insert | MongoDB Java driver

折月煮酒 提交于 2019-12-11 05:57:09
问题 I want to check if an insert fails (due to unique=True index in the collection). If there is an error do something. Bellow is an example of my code. DBCollection user...; BasicDBObject Doc = new BasicDBObject(... ); String user_exists = user.insert(Doc).getError(); //insert the doc get error if any if(user_exists!=null){ //any errors? user.update(new BasicDBObject(...)); // error exists so do smthng } The above as it is does not work. I believe that the String user_exists is always null. How

DBCollection save() equivalent in mongo 3.3

本小妞迷上赌 提交于 2019-12-11 05:29:19
问题 Our current implementation with mongo-java-driver:3.0.4 for a document update is as follows - private void updateParam(String param1, String param2) { Param param = new Param(param1, param2); DBCollection collection = mongoClient.getDB("databaseName").getCollection("collectionName"); collection.save(new BasicDBObject(param.toMap())); } where param.toMap() is implemented as public Map<String, Object> toMap() { return JSONObjectMapper.getObjectMapper().convertValue(this, Map.class); } With

how to check if an document is updated or inserted in MongoDB

我们两清 提交于 2019-12-11 05:21:50
问题 I'm just wondering if there's way to check if a given document is updated or inserted in MongoDB after a upsert operation in Java: DBCollection col = getCollection(); // either this col.findAndModify(query, null, null, false, contact, false, true); // or this WriteResult wr = col.update(query, contact, true, false); 回答1: Generally, WriteResult will hold information regarding whether it was successful or not; if successful, the document will be there. You could also do a simple col.count(new

Exception when geting distinct values of a field from Mongo collection using mongo Java Driver 3.0

冷暖自知 提交于 2019-12-11 03:47:40
问题 I'm getting distinct values of field from mongodb. When i run the following query in the commandline, it works good. db.celldata.distinct("tenentId") I'm using Mongo java 3.0 driver, to retrieve the distinct values using the following query MongoCursor<String> iterator = coll.distinct("tenantId", String.class).iterator(); When i run the query i get the following exception org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when

Mongodb Java query for date range

泄露秘密 提交于 2019-12-11 03:05:00
问题 I needed to find all the records in mongo db within two date ranges using Mongo Driver[3.4.0] for Java. Example: I have books Collection. { "_id" : ObjectId("5acb40d27d63b61cb002bafe"), "title" : "WingsOfFire", "pub-date" : ISODate("2013-10-02T00:00:00.000Z"), "rel-date" : ISODate("2013-11-02T00:00:00.000Z") } Like above I have 100s of documents. I need to find all records wherein pub-date > rel-date. I am using Mongo DB version 3.2.6 I tried to use $expr operator but it seems to work with

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 =

Manual Conversion of 3rd Party Class With Morphia

时间秒杀一切 提交于 2019-12-11 01:40:58
问题 Long story short: Is it possible to write a type converter for a 3rd party library class with Morphia? Long story: I'm new to Morphia. I have an entity class which contains a field typed javax.activation.MimeType . When I try to save instances of my class, Morphia complains it "can't serialize class javax.activation.MimeType". I tried writing a TypeConverter and adding it to the list of converters but it didn't work. Here are the code pieces: Entity.class @Entity @Converters(MimeTypeConverter

Morphia - using hasAllOf and hasThisElement together

坚强是说给别人听的谎言 提交于 2019-12-11 01:22:51
问题 I have a mongo collection named 'comment' which has documents which have a structure similar to this : { "_id" : ObjectId("9873214jkhdkfjdsf8324"), "nm" : "test", "sts" : 1, "updby" : NumberLong(0), "tags" : [ { "name" : "women", "rank" : 1, "type" : 3 }, { "name" : "men", "rank" : 1 }, { "name" : "clothing", "rank" : 2, "type" : 1 } ] I want to query the collection using a mongo query which would look like this : db.comment.find({"tags":{ $all:[ {"$elemMatch":{"name" : "women", "type" : 3}},