mongodb-java

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

我只是一个虾纸丫 提交于 2019-11-28 20:38:52
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", testsuite); testObject.put("testname", testcase); testObject.put("milestones", new BasicDBObject(

Spring Data MongoDB Date Between

怎甘沉沦 提交于 2019-11-28 19:48:00
问题 I use spring data mongodb. I want the records between two dates. The following MongoDB Query works: db.posts.find({startDate: {$gte: start, $lt: end}}); My attempted Spring data query object code translation does not work: Query query = new Query(); query.addCriteria(Criteria.where("startDate").gte(startDate) .and("startDate").lt(endDate)); What is the correct order of method calls to build the Mongo query I need? 回答1: Do not include the ' and("startDate") ' part in your criteria. Instead of

converting Document objects in MongoDB 3 to POJOS

試著忘記壹切 提交于 2019-11-28 12:01:41
I'm saving an object with a java.util.Date field into a MongoDB 3.2 instance. ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(myObject); collection.insertOne(Document.parse(json)); the String contains: "captured": 1454549266735 then I read it from the MongoDB instance: final Document document = collection.find(eq("key", value)).first(); final String json = document.toJson(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); xx = mapper.readValue(json, MyClass.class); the deserialization fails:

How to catch exception when creating MongoClient instance

假如想象 提交于 2019-11-28 08:52:19
问题 I am using Mongo DB java driver to connect to mongo instance. Below is the code I am using to create the MongoClient instance. try { new MongoClient("localhost", 1111); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } If the hostname or port number is not correct, I will get below exceptions. I wander how I can catch that exceptions. MongoDB connecting is happening in an internal thread which can't be catched by client code. I want to know whether the

Mongodb inserting doc without _id field

走远了吗. 提交于 2019-11-28 08:32:15
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: ...} 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.createCollection("noautoid", { autoIndexId: false }) { "ok" : 1 } Then you can insert documents without _id field.

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

一个人想着一个人 提交于 2019-11-28 06:53:32
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 ? I agree with Rhangaun's answer here is my soluction in code of JAVA: public static DB getMongoDB() { MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); //build the connection options builder.maxConnectionIdleTime(60000);//set the max

Mongodb avoid duplicate entries

落爺英雄遲暮 提交于 2019-11-28 06:20:36
I am newbie to mongodb. May I know how to avoid duplicate entries. In relational tables, we use primary key to avoid it. May I know how to specify it in Mongodb using java? Use an index with the {unique:true} option. // everyone's username must be unique: db.users.createIndex({email:1},{unique:true}); You can also do this across multiple fields. See this section in the docs for more details and examples. MongoDB indexes may optionally impose a unique key constraint , which guarantees that no documents are inserted whose values for the indexed keys match those of an existing document. If you

Insert document in mongodb with autoincrement field from java

风格不统一 提交于 2019-11-28 05:15:24
问题 I am trying to insert a document with a sequence number, in one transaction, from java. Something similar to this: function getNextSequence(name) { var ret = db.counters.findAndModify( { query: { _id: name }, update: { $inc: { seq: 1 } }, new: true } ); return ret.seq; } collection.insert({ number: getNextSequence("userid"), name: "Some Name" }); Is it possible to do this from java? Preferably with the official java driver. 回答1: Following the documentation for creating an Auto-Incrementing

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

ぐ巨炮叔叔 提交于 2019-11-27 20:25:22
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, both method putAll(Map) in BasicBSONObject and method putAll(BSONObject) in BasicBSONObject match query

How to update value of specific embedded document, inside an array, of a specific document in MongoDB?

你说的曾经没有我的故事 提交于 2019-11-27 19:54:32
I have the following structure in my document: { _id : ObjectId("43jh4j343j4j"), array : [ { _arrayId : ObjectId("dsd87dsa9d87s9d7"), someField : "something", someField2 : "something2" }, { _arrayId : ObjectId("sds9a0d9da0d9sa0"), someField : "somethingElse", someField2 : "somethingElse2" } ] } I want to update someField and someField2 but only for one of the items in the array, the one that matches _arrayId (e.g. _arrayId : ObjectId("dsd87dsa9d87s9d7") ; and only for this document (e.g. _id : ObjectId("43jh4j343j4j") ) and no other. The arrayIds are not unique to the document that's why I