mongodb-java

Document field names can't start with '$' (Bad Key: '$set')

瘦欲@ 提交于 2019-12-24 14:48:41
问题 BasicDBObject u = new BasicDBObject(); DBObject q = new BasicDBObject(); q.put("orgId", orgId); u.append("orgId", orgId); if(accounts.keySet().size() > 0) { BasicDBObject setObj = new BasicDBObject(); for (String key : accounts.keySet()) { String fieldToUpdate = "accounts." + key; setObj.append(fieldToUpdate, accounts.get(key)); } u.append("$set", setObj); } DBCollection collection = db.getCollection(collectionName); WriteResult result = collection.update(q, u, true, false); I get following

Mongodb java: Perist POJO class with generic field

杀马特。学长 韩版系。学妹 提交于 2019-12-24 03:37:46
问题 I have a POJO class which looks like this- public class CacheEntity<V> { private String cacheId; private V value; public static final String ID_KEY = "cacheId"; public CacheEntity() { } public CacheEntity(String cache_id, V value) { this.cacheId = cache_id; this.value = value; } public V getValue() { return value; } public void setValue(V value) { this.value = value; } public String getCacheId() { return cacheId; } public void setCacheId(String cacheId) { this.cacheId = cacheId; } } I am

How to get size in bytes of bson documents

痴心易碎 提交于 2019-12-23 21:19:48
问题 Is the int value returned by size() function of bson document the number of bytes ? (not able to find the details of this API). How can I get the size of a bson document in bytes? Here is my code. import org.bson.Document; MongoDatabase db...; MongoCollection<Document> mongoCollection = db.getCollection(...); MongoCursor<Document> cursor = mongoCollection.find().iterator(); Document doc = cursor.next(); int size = doc.size(); // does this return number of bytes // how to get size of doc in

JSON.parse() equivalent in mongo driver 3.x for Java

旧街凉风 提交于 2019-12-23 08:56:59
问题 JSON.parse() from mongo (Java driver) returns either a BasicDBList or a BasicDBObject. However, when migrating to mongo driver 3.x, what's the new parse method that returns either Document or List<Document> ? In the new driver, Document.parse() only parses an object, not an array (throws an exception when given an array). What is the equivalent of JSON.parse() for Arrays with 3.x Java drivers ? 回答1: A simple trick to parse any JSON and to get either Document or List<Document> : Document.parse

subtraction in mongo query does not work?

耗尽温柔 提交于 2019-12-23 02:28:48
问题 I have a collection as follow: { "_id" : ObjectId("5491d65bf315c2726a19ffe0"), "tweetID" : NumberLong(535063274220687360), "tweetText" : "19 RT Toronto @SunNewsNetwork: WATCH: When it comes to taxes, regulations, and economic freedom, is Canada more \"American\" than America? http://t.co/D?", "retweetCount" : 1, "source" : "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>", "Date" : ISODate("2014-11-19T04:00:00.000Z"), "Added" : ISODate("2014-11-19T04:00:00.000Z"),

Can't find MongoClient in Java Drivers

元气小坏坏 提交于 2019-12-22 01:39:17
问题 In reading all the documentation on the mongo site for using the Java Driver, it makes references to using MongoClient() to make a connection. It talks about how they formerly used Mongo() but it has been deprecated. However, when I download the latest (or any) version of the java driver that the Mongo database links to (http://central.maven.org/maven2/org/mongodb/mongo-java-driver/), and load it into my project libraries, I cannot import com.mongodb.MongoClient because it isn't there! I've

mongoDB selecting record based on two conditions

霸气de小男生 提交于 2019-12-22 01:37:54
问题 A sample record in my database looks like : { "_id" : 2, "name" : "Corliss Zuk", "scores" : [ { "type" : "exam", "score" : 87.53859552156015 }, { "type" : "quiz", "score" : 24.49132971110967 }, { "type" : "homework", "score" : 99.24881912510654 } ] } I am trying to select all records that have homework scores > 90 or exam scores < 50. My OR condition is like this: DBObject clause1 = new BasicDBObject("scores.type", "homework").append("scores.score", new BasicDBObject("$gt", 90)); DBObject

Spring data mongodb: mongodb repositories not found when autowired

杀马特。学长 韩版系。学妹 提交于 2019-12-21 22:30:23
问题 I have the following stack: Spring MVC web app Spring Data MongoDB When I deploy the webapp and start server (Tomcat 7) I'am getting the following error : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.qopuir.repository.UserRepository com.qopuir.service.UserService.userRepository;

Why do I end up with java.lang.IllegalArgumentException for Casbah / Java MongoDB Driver?

流过昼夜 提交于 2019-12-21 07:00:11
问题 I'm seeing a strange issue using the casbah / java driver. I keep running into the following exception when the driver tries to create a response from mongo: Oct 16, 2012 10:45:07 AM com.mongodb.DBTCPConnector$MyPort error SEVERE: MyPort.error called java.lang.IllegalArgumentException: response too long: 1634610484 at com.mongodb.Response.(Response.java:40) at com.mongodb.DBPort.go(DBPort.java:110) at com.mongodb.DBPort.go(DBPort.java:75) at com.mongodb.DBPort.call(DBPort.java:65) at com

How to get mongo command results in to a flat file

杀马特。学长 韩版系。学妹 提交于 2019-12-20 08:27:31
问题 How do I export the results of a MongoDB command to a flat file For example, If I am to get db.collectionname.find() into a flat file. I tried db.collectionname.find() >> "test.txt" doesnt seem to work. 回答1: you can try the following from the command line mongo 127.0.0.1/db --eval "var c = db.collection.find(); while(c.hasNext()) {printjson(c.next())}" >> test.txt assuming you have a database called 'db' running on localhost and a collection called 'collection' this will export all records