EzDevInfo.com

morphia

MongoDB object-document mapper in Java based on <a href="https://github.com/mongodb/mongo-java-driver">https://github.com/mongodb/mongo-java-driver</a>

Mongo pagination

I have a use case where I need to get list of Objects from mongo based off a query. But, to improve performance I am adding Pagination. So, for first call I get list of say 10 Objects, in next I need 10 more. But I cannot use offset and pageSize directly because the first 10 objects displayed on the page may have been modified [ deleted ].

Solution is to find Object Id of last object passed and retrieve next 10 objects after that ObjectId.

Please help how to efficiently do it using Morphia mongo.


Source: (StackOverflow)

Morphia query with or operator

I want to know how to write a Morphia mongodb query with 'or' operator
I wrote mongodb query like this and this work fine

db.Inv.find({$or:[{sug_id:2},{grp_id:2}]})  

But i got confused when i try to write this in morphia, following query is wrong but how can write something similar to this

List<Inv> invs = ds.find(Inv.class).field("grp_id").hasAnyOf(grpId).or(field("sug_id")).hasAnyOf(grpId).asList();  

Thanks


Source: (StackOverflow)

Advertisements

MongoDB Composite Key

I'm just getting started with MongoDb and I've noticed that I get a lot of duplicate records for entries that I meant to be unique. I would like to know how to use a composite key for my data and I'm looking for information on how to create them. Lastly, I am using Java to access mongo and morphia as my ORM layer so including those in your answers would be awesome.

Morphia: http://code.google.com/p/morphia/


Source: (StackOverflow)

Are persistence annotations in domain objects a bad practice?

I realize that persistence frameworks such as Morphia and Hibernate rely on annotations on domain objects to do their magic. At some level, it seems to me that this is inserting persistence concerns into the domain layer which is something we're supposed to strive to avoid.

Is this something that I should try to dodge by perhaps using an external configuration file or maybe separate DTOs from the domain model? Or is this little leak between the persistence and domain layers generally regarded as acceptable?


Source: (StackOverflow)

Unit testing with MongoDB

My database of choice is MongoDB. I'm writing a data-layer API to abstract implementation details from client applications - that is, I'm essentially providing a single public interface (an object which acts as an IDL).

I'm testing my logic as I go in a TDD manner. Before each unit test, an @Before method is called to create a database singleton, after which, when the test completes, an @After method is called to drop the database. This helps to promote independence amongst unit tests.

Nearly all unit tests, i.e. performing a contextual query, require some kind of insertion logic to occur before hand. My public interface provides an insert method - yet, it seems incorrect to use this method as precursor logic to each unit test.

Really I need some kind of mocking mechanism, yet, I haven't had much experience with mocking frameworks, and it seems that Google returns nothing re a mocking framework one might use with MongoDB.

What do others do in these situations? That is, how do people unit test code that interacts with a database?

Also, my public interface connects to a database defined in a external configuration file - it seems incorrect to use this connection for my unit testing - again, a situation that would benefit from some kind of mocking?


Source: (StackOverflow)

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

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.


Source: (StackOverflow)

What the overhead of Java ORM for MongoDB

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,


Source: (StackOverflow)

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

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?


Source: (StackOverflow)

Difference between _id & $oid ; $date & IsoDate in mongo database

We are using mongo db to store certain records in production database.

We see our records having "_id" : { "$oid" : "50585fbcb046b2709a534502"} in production database , while we see same record as "_id" : ObjectId(" 50585fbcb046b2709a534502 ") in the qa database. For dates we see "ld" : { "$date" : "2011-12-03T17:00:00Z"} in prod database, while "ld" :ISODate("2011-12-03T17:00:00Z") in qa database. We have tested our queries successfully in qa environment, but worried it might fail in production

1) Will my java queries work seamlessly on prod & qa both? (I am using morphia apis to query) 2) Are they internally being stored in the same identical way?


Source: (StackOverflow)

How to make numeric comparison when using morphia to perform an $elemMatch query

My document has the following structure:

{
"scores": [{
    "scoreTitle": "environment",
    "scoreValue": 3,
    "scoreDescribe": "good"
}, {
    "scoreTitle": "service",
    "scoreValue": 3,
    "scoreDescribe": "good"
}, {
    "scoreTitle": "taste",
    "scoreValue": 4,
    "scoreDescribe": "good"
}]
}

In mongo shell, I can use the following query to find the document which has a score whose title is 'environment' and value is greater than 2.

db.reviews.find({"scores":{"$elemMatch":{"scoreValue":{"$gt":2},"scoreTitle":"environment"}}})

Now I want to query the document using morphia, from the api doc, the 'elem' operator is supported in the fiter method, and an additional query criteria object is required, for example, query for document that has a score whose title is "environment" and describe is "good":

Score score = new Score();// the criteria object, only support comparisons of equality
score.setScoreTitle("environment"); // title should be environment
score.setScoreDescribe("good"); // describe should be good
Query q = ds.createQuery(Review.class).filter("scores elem", score);

My question is: How can I make a numeric comparison in the query criteria, that is to say, how can I make a query that has the same effect as the mongo shell counterpart.


Source: (StackOverflow)

Query MongoDB for ordered distinct values

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", 1);
List typeBs = myCol.distinct("TypeB", orderBy);

But in this case the list is empty, where are my assumptions wrong?

UPDATE

By using the CLI I found out that the following query returned the expected result:

> db.mycollection.find({$query : {}, $orderby : { TypeB : 1 }})

So I adjusted my code accordingly:

DBCollection myCol = getDatastore().getCollection(MyCollection.class);

BasicDBObject ascending = new BasicDBObject("TypeB", 1);
BasicDBObject filter = new BasicDBObject();
filter.put("$query", new BasicDBObject());
filter.put("$orderby", ascending);

List typeBs = myCol.distinct("TypeB", filter);

Still result contains 0 entries!

What really makes me confused is that the same query works if I use .find instead of .distinct:

DBCollection myCol = getDatastore().getCollection(MyCollection.class);

BasicDBObject ascending = new BasicDBObject("TypeB", 1);
BasicDBObject filter = new BasicDBObject();
filter.put("$query", new BasicDBObject());
filter.put("$orderby", ascending);

myCol.find(filter).hasNext();  <-- Evaluates to TRUE

Why is it that the filter doesn't work when using the distinct method-call?


Source: (StackOverflow)

how to retrieve last update time of each document in mongodb

I would like to know if there is a way to get the last update/modify timing of a data (i.e documents) in a collection in mongodb. More clearly, i make a query to retrieve all documents updated after a particular time.

Can you help me if there is any possible ways to retrieve this timestamp details in mongodb ?

Note: for newly created documents, I know that we can retrieve the timestamp from the objectId. But for update, the id is going to be same, does mongodb stores anywhere the last update time for each document.

i am using morphia as java driver, so if there is any possibility from morphia also please let me know.. Thanks, Sadish


Source: (StackOverflow)

morphia and howto update existing document field

Im completely new to MongoDb and Morphia and
trying to learn how to update my document.

I cannot see/understand how to do it from this page:
http://www.mongodb.org

My Document looks as following:(could be some error here)

@Entity
public class UserData {

    private Date creationDate;
    private Date lastUpdateDate;

    @Id private ObjectId id;
    public String status= "";
    public String uUid= "";


    public UserData() {
        super();
        this.statistic = new Statistic();
        this.friendList = new FriendList();
    }

    @Embedded
    private Statistic statistic;
    @Embedded
    private FriendList friendList;

    @PrePersist
    public void prePersist() {
        this.creationDate = (creationDate == null) ? new Date() : creationDate;
        this.lastUpdateDate = (lastUpdateDate == null) ? creationDate : new Date();
    }
}

On that page i cannot see any place where they describe howto update my UserData that has a specific uUid
Like update UserData.status if uUid=123567

This is what i think i should use:

ops=datastore.createUpdateOperations(UserData.class).update("uUid").if uuid=foo..something more here..

// morphia default update is to update all the UserData document so howto update selected ones

datastore.update(datastore.createQuery(UserData.class), ops);  

Source: (StackOverflow)

Spring Security authentication using UserDetailsService

I have some issues with Spring security authentication. Everywhere in my application everything works great (CRUD operations work well), but login attempt fails.

Here's my code (I marked below with comments where userDAO is null which is cause of failed authentication):

@Service
public class UserServiceImpl implements UserService, UserDetailsService {

    @Autowired
    UserDAO userDAO;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDAO.getUserByUsername(username); //userDAO == null Causing NPE
        if (user == null)
            throw new UsernameNotFoundException("Oops!");

        List<SimpleGrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority(user.getRole()));

        return new org.springframework.security.core.userdetails
                .User(user.getLogin(), user.getPassword(), authorities);
    }

@Override
    public List<User> getUsers() {
        return userDAO.getUsers();//userDAO !=null
    }
//rest of code skipped

My SecurityConfig looks like this

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

 UserServiceImpl userService = new UserServiceImpl();

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService);
    }
//rest of code skipped

I marked where i get NPE and i have no idea how to solve this. Whole Configuration is JavaBased and you can check it out out here for more details HERE

EDIT: getUsers() is invoked this way in controller:

@Controller
public class LoginController {
    @Autowired
    UserService userService;

 @RequestMapping(value = "/dashboard")
    public ModelAndView userDashboard(){
        ModelAndView modelAndView = new ModelAndView("Dashboard");
        List<User> userList = userService.getUsers();
        modelAndView.addObject("users", userList);
        return modelAndView;
    }

And in this case (when invoking userService.getUsers()) userDAO is not null

Tried to fix it like Bohuslav Burghardt suggested and i got

 method userDetailsService in class org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder cannot be applied to given types;
  required: T
  found: com.gi.service.UserService
  reason: inferred type does not conform to upper bound(s)
    inferred: com.gi.service.UserService
    upper bound(s): org.springframework.security.core.userdetails.UserDetailsService

in line auth.userDetailsService(userService);


Source: (StackOverflow)

Storing multidimensional arrays with Morphia

I have troubles reading/unmarshalling multidimensional arrays with Morphia.

The following class:

@Entity
class A {

  double[][] matrix;
}

is properly marshalled and stored in mongodb, but when reading it I get an exception that the double[][] can not be constructed. I've tried to use a custom TypeConverter but it is not getting invoked for such types. Similar issues I get when using a member like this:

List<double[]> matrix;

I did not find any annotations that could help morphia figure out what type is expected in the array. I suspect this is not supported yet. Any suggestions ?

Thanks in advance.


Source: (StackOverflow)