我是mongodb的新手,我有一个像这样的JSON:
{
"first_name" : "John",
"last_name" : "Smith",
"address" : {
"street" : "123 Main Street",
"city" : "Anytown",
"state" : "NY"
}
}
现在我想查找“street”=“123”(比如说)的所有文件。在mongo shell中,我按如下方式执行:
db.collection_name.find(
{
'address.street' : '123'
}
)
在Java中,如果我想查找“firstname”=“John”的文档,我会这样做:
BasicDBObject nameQuery = new BasicDBObject();
nameQuery.put("firstname", "John");
DBCursor cursor = collection.find(nameQuery);
我无法找到adress.street,我尝试了一些东西,但它没有用。
我试过了:
addressQuery.put("address.street", "123");
答案 0 :(得分:0)
你的建议应该起作用。这对我有用:
DBCollection coll = db.getCollection(...);
DBCursor cursor = coll.find(new BasicDBObject("address.street", "123 Main Street"));
while (cursor.hasNext()) {
System.out.println(cursor.next()); //prints the object
}