morphia

Query MongoDB for ordered distinct values

☆樱花仙子☆ 提交于 2019-12-05 04:04:37
I am using Morphia Java driver for querying a MongoDB that contains a collection of the following form: MyCollection { TypeA TypeB } I want to retrieve all distinct values of TypeB which I do using the following code: DBCollection myCol = getDatastore().getCollection(MyCollection.class); List typeBs = myCol.distinct("TypeB"); Above code works as expected, but list of distinct values is of course not sorted. I have experimented with the following code: DBCollection myCol = getDatastore().getCollection(MyCollection.class); DBObject orderBy = new BasicDBObject("$orderby", new BasicDBObject("TypeB

Can I use String as ID type for mongodb document?

我是研究僧i 提交于 2019-12-04 23:34:39
I am using java/morphia to deal with mongodb. The default ObjectId is not very convenient to use from Java layer. I would like to make it a String type while keep the key generation process using ObjectId, say _id = new ObjectId.toString() . I want to know if there is any side effects doing it this way? For example, will it impact the database performance or causing key conflicts in any means? Will it affect the sharding environment ... You can use any type of value for an _id field (except for Arrays). If you choose not to use ObjectId, you'll have to somehow guarantee uniqueness of values

MongoDB elemMatch subdocuments

淺唱寂寞╮ 提交于 2019-12-04 11:44:31
I have the following data structure { "_id" : ObjectId("523331359245b5a07b903ccc"), "a" : "a", "b" : [ { "c" : { "_id" : ObjectId("5232b5090364678515db9a82"), "d" : "d1" } }, { "c" : { "_id" : ObjectId("5232b5090364678515db9a83"), "d" : "d2" } } ] } For the following queries, mongo returns > db.test.find({b : {$elemMatch : {'c.d': 'd1'}}}).count(); 1 > db.test.find({b : {$elemMatch : {c: {d: 'd1'}}}}).count(); 0 Unfortunately, for the following statements B b = new B(); C c = new C(); b.c = c; b.c.d = "d1"; createQuery().field("b").hasThisElement(b).asList(); Morphia generates db.test.find({b

How can I sort MongoDB query results by inner array size?

柔情痞子 提交于 2019-12-04 09:57:09
I'm using Morphia to access mongoDB. I need to get a list of objects by the length of the inner array. Does any one have an idea how it can be done without getting all the collection to Java and sort it there? You should create extra field with nested array size and use $inc to update this field. Also you can use $where , but it very slow. You search by nested array length like this: db.coll.find({ $where: "this.nestedArray.length > 3" }); But as i said better to create an extra field. tsinik OK I found it :-) dataStore.find(MyClass.class).order("-inner_array.length").asList(); does the trick.

How to prefer reads on secondaries in MongoDb

三世轮回 提交于 2019-12-04 07:41:06
When using mongodb in a replica set configuration (1 arbiter, 1 primary, 2 slaves); how do I set a preference that read be performed against the secondaries and leave the primary only for writes? I'm using MongoDb 2.0.4 with Morphia. I see that there is a slaveOk() method, but I'm not sure how that works. Morphia http://code.google.com/p/morphia/ Details My Mongo is set with the following options: mongo.slaveOk(); mongo.setWriteConcern(WriteConcern.SAFE); I am attempting to use the following (this may be answer -btw): Datastore ds = getDatastore(); Query<MyEntity> query = ds.find(MyEntity

How to retrieve last update time of each document in MongoDB?

▼魔方 西西 提交于 2019-12-03 10:47:31
I would like to know if there is a way to get the last update/modify time of data (i.e documents) in a collection in MongoDB. More clearly, I want to make a query to retrieve all documents updated after a particular time. Is there any possible ways to retrieve this last modified timestamp in MongoDB? Note: For newly created documents, I know that we can retrieve the timestamp from the objectId, but for an update, the id is going to be same. Does MongoDB store the last update time for each document anywhere? I am using morphia as the java driver, so if there is any possible way from morphia,

What the overhead of Java ORM for MongoDB

China☆狼群 提交于 2019-12-03 09:43:08
问题 What is the overhead of using Java ORM for MongoDB, or its better that we go the at basic driver level to read or write? We will be adding Mongo DB for one of our requirements. There are couple of java ORM mapping tools for java -morphia -spring-data -others Morphia last version was released more than a year ago but Spring data is actively maintained. Which one should be used if I am about to start now, 回答1: Using ORM decreases the performance but it speed up the development. There is a trade

Is there any orm-like library for mongodb in scala?

半腔热情 提交于 2019-12-03 06:29:17
It seems only the casbah we can use in scala, but I hope there is a orm-like library for scala, like morphia for java, or something else. Is there any? I don't want to use morphia in scala because I have to convert java collections to scala UPDATE I've tried some of them, but still not find a proper one. Some are hard for scala newbies to get started. FINALLY Finally, I chose mongo-scala-driver , its awesome. Thanks to everybody. There are two solid options: Salat, which is designed to integrate with Casbah using case classes and scalasig - https://github.com/novus/salat/ Lift (liftweb.net)

What the overhead of Java ORM for MongoDB

夙愿已清 提交于 2019-12-03 00:07:31
What is the overhead of using Java ORM for MongoDB, or its better that we go the at basic driver level to read or write? We will be adding Mongo DB for one of our requirements. There are couple of java ORM mapping tools for java -morphia -spring-data - others Morphia last version was released more than a year ago but Spring data is actively maintained. Which one should be used if I am about to start now, Parvin Gasimzade Using ORM decreases the performance but it speed up the development. There is a trade off here. For ORM tools, Morphia is most stable one. Here you can find the comparison

Morphia MongoDB check for null and non existing field

别说谁变了你拦得住时间么 提交于 2019-12-01 06:25:46
问题 I am new to both Morphia and MongoDB. Is there a way to check using Morphia that a certain field in my database is not null and also exists. For example from the following record of a user from a collection of users in database: { "_id" : ObjectId("51398e6e30044a944cc23e2e"), "age" : 21 , "createdDate" : ISODate("2013-03-08T07:08:30.168Z"), "name" : "Some name" } How would I use a Morphia query to check if field "createdDate" is not null and exists. EDIT: I am looking for a solution in