FAQ Database Discussion Community


Jongo MongoDB: How to specify a default ReadPreference

java,mongodb,mongodb-java,jongo
I am aware that I can set the readPreference for each query using <collection>.withReadPreference(primaryPreferred()).find(...). However, I would prefer to set a global default readPreference to use "nearest" if possible, and then be able to override this for individual queries if necessary. Is there a possible/preferred way to do this using...

multiplication of 3 value extracted from mongodb in java

java,mongodb,mongodb-query,mongodb-java
I have a document in mongodb as follow: "sentiment" : { "Value" : 0, "high" : 0 }, "quality" : { "Value" : 0, "high" : 0 }, "intensity" : { "Value" : 0, "low" : 0 } Now I want to multiply quality.valueintensity.valuesentiment.value and then return the result here...

MongoDB select's/find seems to have the same performance for a larger amount of data

java,database,mongodb,mongodb-java,mongo-java
I'm measuring the performance of MongoDB with its native driver. For the insert operation, the more I inserted the more time it took. However, with selecting, I have 500.000 documents (inside 1 collection) with each document having 4 other embedded, small documents inside of it. I performed a select query...

How to upsert document in MongoDB Java driver 3

java,mongodb,scala,mongodb-java,casbah
what is the idiomatic way to upsert a document using version 3 of the mongodb java driver (specifically v3.0.1)? We have a collection for sessions and when a new session gets created or modified, we want to upsert it in one operation - rather than having to query if a...

MongoDB Auto-Incrementing id field with java mongo driver?

java,dao,mongodb-java,morphia
Whole day I have tried to find a answer on the question: "How to add auto-Incrementing "id" field in an Entity class?". I am using Morphia (a type-safe java library for Mongo DB). After a couple hours of digging in source code and googling I have found a LongIdEntity class...

mongo java 3.0 driver remove all

mongodb-java
I don't want to drop the collection and recreate it, though if that is the recommended way, I guess I'll do that. It used to be there was a collection.removeAll() method... there is a collection.deleteMany( bson ) , but is there a "match-all" Filter available? (my first pass through I...

Using MongoDB API in a Web application

mongodb-java
I'd need to use the MongoClient and DB objects repeatedly in a Web application: MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB( "test" ); Is it safe to cache and re-use these objects among different clients accessing our application? Thanks...

How to create a compound text index in MongoDB using java driver

mongodb,mongodb-java
I have a requirement to create a compound text index on two fields. I am using java driver. I found examples on how to create index for compound fields but not for text indexes. How can I achieve this using java driver

Mockito.any() for

java,unit-testing,mockito,mongodb-java,springmockito
I want to mock a method with signature as: public <T> T documentToPojo(Document mongoDoc, Class<T> clazz) I mock it as below: Mockito.when(mongoUtil.documentToPojo(Mockito.any(Document.class), Mockito.any(WorkItemDTO.class))) But I get error as: The method documentToPojo(Document, Class) in the type MongoUtil is not applicable for the arguments (Document, WorkItemDTO) Is there any method in Mockito...

With the mongodb-java-api, is it possible to “select” the objectID and return the value from a Mongodb.find operation in one operation?

mongodb,mongodb-query,mongodb-java
I am interested in to find the id from a single document as the result of a find operation. In Robomongo I can get the value in question with this statement: db.collection.find({ "field" : {$exists: true}}).limit(1).next()._id.valueOf() What is the minimal code in Java I have to use to get the...

MongoDB Java Driver 3.0 MapReduce

mongodb,mongodb-java
This is the code I'm using to run map reduce on sourceCollectionName and to get the output to targetCollectionName. But the targetCollectionName is never created. new MongoClient("localhost").getDatabase(dbName).getCollection(sourceCollectionName) .mapReduce(map, reduce) .action(MapReduceAction.REPLACE) .databaseName(dbName) .collectionName(targetCollectionName) .sharded(false); Although I'm able to get output as MapReduceIterable and when I iterate this the result is dumped...

subtraction in mongo query does not work?

mongodb,mongodb-query,mongodb-java
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"...

How to use 'or' along with 'and' operation in mongodb using its java driver?

mongodb,mongodb-java
db.mycol.find( { "likes": { $gt: 10 }, $or: [ {"by": "tutorials point"}, {"title": "MongoDB Overview"} ] }).pretty() what would be its equivalent java code? ...

Update to replace all fields in Spring Data MongoDB

spring-data-mongodb,mongodb-java
MongoDB has the capability to update a document by replacing all of the fields. This is done by not passing any modifier operators. I would like to do the same operation in Spring Data MongoOperations update* methods. In the JavaDoc, it seems that this is supported out of the box:...

What package do I need to import for the Jackson mongodb DBquery functions to compile in Java?

java,mongodb,mongodb-query,mongodb-java,mongo-jackson-mapper
I have this query string : coll.find(and(DBQuery.greaterThanEquals("createdOn", startDate), DBQuery.lessThanEquals("createdOn", endDate),DBQuery.is("user_email",userEmail))); I am getting the error : cannot find symbol [symbol: variable DBQuery] [location: class models.food.UserMeal] Now a plain DB query coll.find(); is working smoothly. What package am I missing for this to work? Update : I tried importing : import...

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

mongodb,spring-data-mongodb,mongodb-java,mongodb-indexes
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 java class representing the term collection as Term.java @Document public class Term{ @Id protected String termId; @Indexed protected...

Retrieve MongoDB data and store it in a list

java,mongodb,mongodb-java
I just started using Java and MongoDB. I want to retrieve some data from my database and store it in a list. I want to get a list with only coordinates. (see sample JSON code). I believe I currently have a list with all the objects in the collection. I...