Questions tagged [mongodb-java]

MongoDB is a scalable, high-performance, open source, document-oriented database. This tag is used for questions related to the Java language bindings and drivers for MongoDB.

Java Language Center

Java Driver

Basics

Specific Topics and How-To

Reference

1045 questions
277
votes
19 answers

How do I partially update an object in MongoDB so the new object will overlay / merge with the existing one

Given this document saved in MongoDB { _id : ..., some_key: { param1 : "val1", param2 : "val2", param3 : "val3" } } An object with new information on param2 and param3 from the outside world needs to be saved var…
Eran Medan
  • 44,555
  • 61
  • 184
  • 276
84
votes
11 answers

Case insensitive sorting in MongoDB

How can I sort a MongoDB collection by a given field, case-insensitively? By default, I get A-Z before a-z.
Varun Kumar
  • 1,015
  • 2
  • 9
  • 17
52
votes
5 answers

Difference between createIndex() and ensureIndex() in java using mongodb

What is the difference between createIndex() and ensureIndex() in Java using MongoDB? I googled this but didn't get a satisfactory answer.
Amit Chahar
  • 2,519
  • 3
  • 18
  • 23
52
votes
12 answers

Spring Data MongoDB Date Between

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…
ujava
  • 1,846
  • 5
  • 20
  • 24
51
votes
8 answers

How to get mongo command results in to a flat file

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.
QVSJ
  • 1,165
  • 2
  • 12
  • 22
48
votes
8 answers

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

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…
user837208
  • 2,487
  • 7
  • 37
  • 54
41
votes
2 answers

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

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…
Bryan
  • 1,431
  • 1
  • 14
  • 19
40
votes
3 answers

Mockito.any() for

I want to mock a method with signature as: public T documentToPojo(Document mongoDoc, Class clazz) I mock it as below: Mockito.when(mongoUtil.documentToPojo(Mockito.any(Document.class), Mockito.any(WorkItemDTO.class))) But I get error…
Rajesh Kolhapure
  • 741
  • 1
  • 10
  • 21
35
votes
5 answers

How to insert multiple documents at once in MongoDB through Java

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…
user663724
31
votes
7 answers

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

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.…
Rhangaun
  • 1,430
  • 2
  • 15
  • 34
30
votes
7 answers

Mongodb avoid duplicate entries

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?
Jessie
  • 963
  • 5
  • 16
  • 26
26
votes
1 answer

MongoDB python bindings an order of magnitude slower than java?

I asked this same question on the mongodb-user list: http://groups.google.com/group/mongodb-user/browse_thread/thread/b3470d6a867cd24 I was hoping someone on this forum might have some insight... I've run a simple experiment comparing the…
Sam
  • 261
  • 2
  • 4
24
votes
4 answers

How to delete all documents in mongodb collection in java

I want to delete all documents in a collection in java. Here is my code: MongoClient client = new MongoClient("10.0.2.113" , 27017); MongoDatabase db = client.getDatabase("maindb"); db.getCollection("mainCollection").deleteMany(new…
Viratan
  • 503
  • 3
  • 9
  • 19
24
votes
3 answers

MongoException: Index with name: code already exists with different options

I have a mongodb collection term with following structure { "_id" : "00002c34-a4ca-42ee-b242-e9bab8e3a01f", "terminologyClass" : "USER", "code" : "X67", "terminology" : "some term related notes", "notes" : "some notes" } and a…
faizi
  • 1,265
  • 2
  • 12
  • 28
23
votes
3 answers

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

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…
m1h4
  • 1,139
  • 2
  • 13
  • 22
1
2 3
69 70