簡體   English   中英

如何在文檔的子數組中使用mongodb java使用mongoDB查找文檔?

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

這真的很難。 我想找到select nodes.index where radio.mac_address="00:06:5A:03:2E:5B" 如何使用Java在MongoDB中獲得此查詢? 我的mongodb如下。

在此處輸入圖片說明

我嘗試了很多查詢。 其中之一如下

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

更新先解決

我還如何編寫更新查詢,例如update rssiLocal="90" where mac_address="00:06:5A:03:2E:5B"

在我的應用程序中,我使用以下方法。 它根據給定的id找到一個對象,並通過Gson對其進行解析並將其保存到相應的對象中。

 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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM