Question

I would like to use mongodb.core.query.Criteria for following request:

db.accounts.find({ "personalSettings.nickName" : "testNickName"})

My scheme of document is:

{
 "_id" : ObjectId("5354de90ad9b0f6c60bef7ba"),
  "personalSettings" : {
    "avatar" : "http://127.0.0.1:8080/assets/samples/avatar.png",
    "nickName" : "testNickName"
},
  "userName" : "testUser@testDomain.com"
}    

I've tried to use this:

Query query = new Query(Criteria.where("personalSettings.nickName").is("testNickName"));

but i got:

java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null

personalSettings is a Map of < String,String >

Thanks

Was it helpful?

Solution

@Override
public Account getAccountByNickName(String nickname, String accountsCollection) {
    Criteria criteria = Criteria.where("personalSettings.nickName").is(nickname);
    DBCollection coll = mongoOperation.getCollection(accountsCollection);
    DBCursor dbCursor = coll.find(Query.query(criteria).getQueryObject());
    DBObject dbobj = dbCursor.next();
    Account account = (new Gson()).fromJson(dbobj.toString(), Account.class);
    return account;
}

For now - only one way :) Because MongoDB doesn't support searching in map by elemMatch and doesn't support searching by ongoTemplate/MongoOperation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top