mongodb-java

Spring Data-MongoDb Options for aggregation method

我是研究僧i 提交于 2019-12-01 23:22:54
How can i set " allowDiskUse " option in aggregation method in spring data-mongodb framework ? The core aggregation abstraction in Spring Data MongoDB is - as the name suggests - Aggregation . It exposes a fluent API to build up a pipeline using aggregation operations. As of version 1.6.0.M1 the Aggregation class has a ….withOptions(…) method to be used like this: Aggregation aggregation = newAggregation(…) // build up pipeline in here .withOptions(newAggregationOptions().allowDiskUse(true).build()); I found spring data-mongodb to have limited support for the aggregation framework, however you

Conversion from String to MongoDB ObjectID

与世无争的帅哥 提交于 2019-12-01 22:38:42
I tried converting my String ID to MongoDB ObjectID public class relevancy_test extends Object implements Comparable<ObjectId> { public static void main(String[] args) throws UnknownHostException { MongoClient mongo = new MongoClient("localhost", 27017); DB mydb = mongo.getDB("test"); DBCollection mycoll = mydb.getCollection("mytempcoll"); BasicDBObject query = null; Map<ObjectId, DBObject> updateMap = new HashMap<ObjectId, DBObject>(); List<DBObject> dbobj = null; DBCursor cursor = mycoll.find(); dbobj = cursor.toArray(); for (DBObject postObj : dbobj) { String id = postObj.get("_id")

MongoDB Slow query by ID

与世无争的帅哥 提交于 2019-12-01 21:14:09
I recently migrated my MongoDB database from Windows Server to CentOS. The version is 2.4.9. I noticed that I have a slow retrieval of records by _id field! I ran a repair database over the weekend bu that did not solve the problem. I have this method that retrieves records by their IDs (with IN operator) (using spring data mongodb 1.4.1.RELEASE): @Override public Map<String, Record> findAsMapIds( final String[] ids, final ComponentType... comps ) { if( null == ids || 0 == ids.length ) { return null; } Map<String,Record> result = new HashMap<String,Record>(); final Criteria cr = where("_id")

Sort documents in collections while fetching

强颜欢笑 提交于 2019-12-01 20:57:51
I have a collection called at MongoDB called resource. It has the following documents: { "_id" : "Abb.e", "_class" : "Resource", "resourceEmail" : "test1@test.com" } { "_id" : "Dasd.tt", "_class" : "Resource","resourceEmail" : "test2@test.com" } { "_id" : "Bbb.rr", "_class" : "Resource", "resourceEmail" : "test3@test.com" } At Java code,I list them as follows: MongoOperations mongoOperations = mongoConfiguration.getMongoTemplate(); List<Resource> resourceList = mongoOperations.findAll(Resource.class); return resourceList; How could I fetch these documents sorted by ID ! As you're using Spring

POJO to org.bson.Document and Vice Versa

亡梦爱人 提交于 2019-12-01 16:02:19
Is there a simple way to convert Simple POJO to org.bson.Document? I'm aware that there are ways to do this like this one: Document doc = new Document(); doc.append("name", person.getName()): But does it have a much simpler and typo less way? The point is, that you do not need to put your hands on org.bson.Document. Morphia will do all that for you behind the curtain. import com.mongodb.MongoClient; import org.mongodb.morphia.Datastore; import org.mongodb.morphia.DatastoreImpl; import org.mongodb.morphia.Morphia; import java.net.UnknownHostException; ..... private Datastore createDataStore()

MongoDB: WriteResult.getN() always returns 0?

我的未来我决定 提交于 2019-12-01 15:29:38
According to the javaDoc , getN() method of WriteResult class in MongoDB-java returns the number of documents updated in the opertion. But it always returns zero, even if the document is inserted correctly. Why so? or I understood it wrongly? I was under the impression that this was the normal MongoDB behaviour, and has nothing to do with the Java driver. The only thing I can find in the documentation is this : getLastError.n reports the number of documents updated or removed, if the preceding operation was an update or remove operation. An insert being neither an update nor a remove , n doesn

MongoDB: WriteResult.getN() always returns 0?

試著忘記壹切 提交于 2019-12-01 15:12:53
问题 According to the javaDoc, getN() method of WriteResult class in MongoDB-java returns the number of documents updated in the opertion. But it always returns zero, even if the document is inserted correctly. Why so? or I understood it wrongly? 回答1: I was under the impression that this was the normal MongoDB behaviour, and has nothing to do with the Java driver. The only thing I can find in the documentation is this: getLastError.n reports the number of documents updated or removed, if the

How to retrieve a single field from MongoDB in Java

别来无恙 提交于 2019-12-01 14:23:56
I am trying to retrieve a field from a document and set it equal to a variable so when I update it later I can just do (field + newCount) or something like that. For example my document is: { "username":"testusername" "item":"Sofa" "price":150.0 } How do I retrieve the price field and set it equal to let's say double currentPrice; ? The documentation is a bit ambiguous so an example would help. Also, I am using mongoDB Java version 3.0.2 Thanks EDIT: Apparently the question is a bit ambiguous, but I am trying to retrieve the price of the sofa that is stored in the database, and set it equal to

Mongodb database Schema Design with shared data

时光总嘲笑我的痴心妄想 提交于 2019-12-01 09:37:26
Hi I am newbie to mongodb.I am using java. I have 4 tables Tenant,system,authorization in my relational table. Something like this. Table Fields Tenant Tenant_ID(PK), Tenant_INFO System System_ID(PK), System_Info Authorization System_ID, Autho_Info. System_prop System_ID, Prop_Info, Tenant_ID In System_prop table, Tenant_ID refers the Tenant Table Tenant_ID (PK), System_ID refers the System Table System_ID. In Authorization table, System_ID refers System tabel System_ID I am switching my database from relational to mongodb. First thing I need to do is Schema design. Query I need to do is:

PersistenceConstructor argument variable name doesn't match instance variable name

梦想的初衷 提交于 2019-12-01 06:03:58
I'm trying to persist the following object with spring-data-mongodb version 1.1.1.RELEASE : @Document public static class TestObject { private final int m_property; @PersistenceConstructor public TestObject(int a_property) { m_property = a_property; } public int property() { return m_property; } } I get a MappingException when I try to read the object back from the database (see full stacktrace below) The naming convention my group uses requires argument variable names to be prefaced by a_ and instance variable names to be prefaced by m_ . It seems like spring-data-mongodb is making the