mongodb - Mongo Java finding Maching record in mongo array -


my sample doc is

{     "_id" : objectid("51f89c1484ae3aa758ad56e9"),     "group_name" : "operator",     "privileges" : [         "add group",         "edit/delete group",         "add user",         "edit/delete user",         "log history",         "add stopwords group",         "edit/delete stopwords group",         "add stopwords",         "edit/delete stopwords",         "recomended stopwords"     ],     "users" : [         {             "full_name" : "operator1",             "user_name" : "operator",             "password" : "pass$123",             "status" : "active"         },         {             "full_name" : "sumit dshpande",             "user_name" : "dsumit",             "password" : "pass$123",             "status" : "active"         }     ] } 

i trying match user_name , password in users array entered user.

my java code :

basicdbobject query = new basicdbobject(); list<basicdbobject> parts = new arraylist<basicdbobject>(); parts.add(new basicdbobject("users.user_name", uname)); parts.add(new basicdbobject("users.password", password)); query.put("$and", parts);  dbcursor cursor = collection.find(query); while (cursor.hasnext()) {     system.out.println(cursor.next()); } 

but returns users in matching array.i want users under group can change group privileges. right approach schema designing in nosql.

we need holistic information application in order tell if you're making right modeling decisions; however, can current query.

in order match multiple attributes within same element of array, should use $elemmatch operator (http://docs.mongodb.org/manual/reference/projection/elemmatch/).

eg)

collection.find({users:{$elemmatch:{username:uname,password:password}}}); 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -