{ "_id": { "$oid": "50a5e1cd703d7e9c65326bf9" }, "name":"arthur", "tele": "001-837475" "address":{ "country":"us", "state" : "CA", "city" : "LA" } } }
I have a lot of such data stored in mongodb. I have such retrieval requirements. Find all people from California.
In the shell, this is query:
db.test.find({"address.state":"CA"})
And can return the correct results.
I want to use mongodb-java-builder to retrieve the database. How to write the code?
This problem has puzzled me for a long time.
This is a link to stackoverflow for my question.No one answered either. The answer didn’t work either.
Thank you.
Mongo db = new Mongo("localhost", 27017); DBCollection coll = db.getCollection("collectionname"); DBObject query = new BasicDBObject("address.state","CA"); DBCursor cursor = coll.find(query); while(cursor.hasNext()){ System.out.println(cursor.next()); } cursor.close()It is because I did not have a good look at the documents. I made such a mistake. I shouldn’t. I should reflect on it here.