mongodb-java

How to add an array to a MongoDB document using Java?

丶灬走出姿态 提交于 2019-12-17 23:12:27
问题 I want to create the following document schema in mongoDB using the java driver { "_id": { "$oid": "513e9820c5d0d8b93228d7e8" }, "suitename": "testsuite_name", "testname": "testcase_name", "milestones": [ { "milestone_id": "359", "testplans": [ { "pland_id": "965", "runs": [ 6985, 5896 ] }, { "plan_id": "984", "runs": [ 9856, 3684 ] } ] } ] } I have the following code BasicDBObject testObject = new BasicDBObject(); BasicDBObject milestoneObject = new BasicDBObject(); testObject.put("suitename

Mongodb inserting doc without _id field

让人想犯罪 __ 提交于 2019-12-17 18:42:19
问题 I am newbie to mongodb. I need to insert a doc without the _id field generating automatically. I need to set the field Tenant_id as unique or need to change the "_id" field to Tenant_id. how to do it? something like this Tenant {Tenant_id: 123, Tenant_info: ...} 回答1: By default, all regular collections automatically insert an _id field if it is absent. However, this behavior can be changed when you create the collection, by setting explicitely the autoIndexId parameter to false. > db

MongoSocketReadException: Prematurely reached end of stream (after a period of inactivity)

岁酱吖の 提交于 2019-12-17 18:19:10
问题 I get this error on a find call (default Java Driver) after a period of inactivity. I tried to add a manual heartbeat (writing to a capped collection), but it didn't help. I only get the issue while being connected to an instance on compose (i.e. not in a local context). MongoDB version is 3.2.8, latest driver (3.3), using Java 8. Any idea ? 回答1: I agree with Rhangaun's answer here is my soluction in code of JAVA: public static DB getMongoDB() { MongoClientOptions.Builder builder = new

How to query documents using “_id” field in Java mongodb driver?

这一生的挚爱 提交于 2019-12-17 15:53:44
问题 I am trying to find documents in MongoDB by searching on "_id" key. My document looks like this- { "_id" : ObjectId("4f693d40e4b04cde19f17205"), "hostname" : "hostnameGoesHere", "OSType" : "OSTypeGoesHere" } I am trying to search this document as- ObjectId id= new ObjectId("4f693d40e4b04cde19f17205"); BasicDBObject obj = new BasicDBObject(); obj.append("_id", id); BasicDBObject query = new BasicDBObject(); query.putAll(query); But I get below error- error: reference to putAll is ambiguous,

Query with sort() and limit() in Spring Repository interface

落爺英雄遲暮 提交于 2019-12-17 15:42:51
问题 I'm new to Spring Data with MongoDB and would like to have an automagically generated query method inside my MongoRepository extension interface which requires filtering, sorting and limiting. The query looks like this: // 'created' is the field I need to sort against find({state:'ACTIVE'}).sort({created:-1}).limit(1) The repository interface looks like this: public interface JobRepository extends MongoRepository<Job, String> { @Query("{ state: 'ACTIVE', userId: ?0 }") List<Job>

mongodb 3.x driver Android compatibility

二次信任 提交于 2019-12-17 13:24:34
问题 I'm developing an Android app that use the java mongodb driver 3.0.3 to connect to every instance of mongodb. The connector code to a generic istance is: try{ MongoCredential credential = MongoCredential.createCredential(user, dbname, pass.toCharArray()); MongoClient mongoClient = new MongoClient( new ServerAddress(server , port ), Arrays.asList(credential)); MongoDatabase db = mongoClient.getDatabase(dbname); System.out.println("Connect to database successfully "); Iterator i= mongoClient

mongodb 3.x driver Android compatibility

◇◆丶佛笑我妖孽 提交于 2019-12-17 13:24:01
问题 I'm developing an Android app that use the java mongodb driver 3.0.3 to connect to every instance of mongodb. The connector code to a generic istance is: try{ MongoCredential credential = MongoCredential.createCredential(user, dbname, pass.toCharArray()); MongoClient mongoClient = new MongoClient( new ServerAddress(server , port ), Arrays.asList(credential)); MongoDatabase db = mongoClient.getDatabase(dbname); System.out.println("Connect to database successfully "); Iterator i= mongoClient

How to insert multiple documents at once in MongoDB through Java

↘锁芯ラ 提交于 2019-12-17 07:22:15
问题 I am using MongoDB in my application and was needed to insert multiple documents inside a MongoDB collection . The version I am using is of 1.6 I saw an example here http://docs.mongodb.org/manual/core/create/ in the Bulk Insert Multiple Documents Section Where the author was passing an array to do this . When I tried the same , but why it isn't allowing , and please tell me how can I insert multiple documents at once ?? package com; import java.util.Date; import com.mongodb.BasicDBObject;

MongoDB - Error “Too many results for query, truncating output” with $geoNear

给你一囗甜甜゛ 提交于 2019-12-13 17:21:27
问题 I'm running a $geoNear query on my sharded cluster (6 nodes with 3 replica sets each of 2 shardsvr and 1 arbiter). I expect the query to return 1.1m documents. I am recieving only ~130.xxx documents. I am using the Java driver to issue the query and process the data (for now, I'm just counting the documents that get returned). I am using MongoDB 3.2.9 and the latest java driver. The mongod log shows the following error which is caused by the output document getting larger than 16MB: 2016-10

How to reference GridFSFile with @DbRef annotation (spring data mongodb)

蓝咒 提交于 2019-12-13 14:34:20
问题 i have a spring @Document object Profile i would like to reference GridFSFile like it : @DbRef private GridFSFile file; the file is writen into another collection type GridFS. I always have a java.lang.StackOverflowError when i set profile.setFile(file); java.lang.StackOverflowError at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:336) at org.springframework.data.util.TypeDiscoverer.hashCode(TypeDiscoverer.java:365) at org.springframework.data.util