how to find document with mongoDB using mongodb java in sub array of document?

廉价感情. 提交于 2019-12-13 04:49:32

问题


This is really hard. I want to find the select nodes.index where radio.mac_address="00:06:5A:03:2E:5B". how can I get this query in MongoDB using java? My mongodb is as following.

I tried so many queries. one of them is as following

BasicDBObject query = new BasicDBObject("nodes.radios.mac_address", "mac_address value";
BasicDBObject fields = new BasicDBObject("nodes.$", 1);
DBCursor cursor = node_info.find(query, fields);

Updated First on got solved

How can i also write update query like update rssiLocal="90" where mac_address="00:06:5A:03:2E:5B".


回答1:


In my application I use following method. It finds an object according to given id parses it via Gson and saves it to corresponding object.

 public MyEntity getValue(String id) {
        DB db = SessionManager.getInstance().db;
        DBCollection collection = db.getCollection("myCollection");
        BasicDBObject query = new BasicDBObject("_id", id);
        Object object = new Object();
        boolean found = false;

        DBCursor cursor = collection.find(query);
        try {
            while (cursor.hasNext()) {
                found = true;
                String str = (cursor.next().toString());
                Gson gson = new Gson();

                object = gson.fromJson(str, MyEntity.class);

            }
        } finally {
            cursor.close();
        }
        if (!found)
            return new MyEntity();
        MyEntity myEntity = null;
        myEntity = (MyEntity) object;
        return myEntity;
    }


来源:https://stackoverflow.com/questions/29625241/how-to-find-document-with-mongodb-using-mongodb-java-in-sub-array-of-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!