Home  >  Q&A  >  body text

query - mongodb如何只根据内嵌文档里的键值对来检索出整个document

{
    "_id": {
        "$oid": "50a5e1cd703d7e9c65326bf9"
    },
    "name":"arthur",
    "tele": "001-837475"
    "address":{
                "country":"us",
                "state" : "CA",
                "city" : "LA"
               }
    }
}

我的mongodb中存了很多这样的数据.我有这样的检索需求.查找所有来自加州的人.
在shell里是这样query:

    db.test.find({"address.state":"CA"})

并且能够返回正确的结果.

我想用mongodb-java-builder来检索数据库.如何书写代码呢?
这个问题已经困扰了我好久了.

这是我的问题的stackoverflow的链接, 也没有人回答.那个回答也不管用.

谢谢了.!

PHPzPHPz2711 days ago645

reply all(1)I'll reply

  • 阿神

    阿神2017-04-21 11:18:21

        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()
        

    I didn’t read the document carefully. I made such a mistake. I shouldn’t, reflect on it here.

    reply
    0
  • Cancelreply