如何在文档的子数组中使用mongodb java使用mongoDB查找文档? [英] how to find document with mongoDB using mongodb java in sub array of document?

查看:123
本文介绍了如何在文档的子数组中使用mongodb java使用mongoDB查找文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

更新首先解决了

我怎么也可以编写像update rssiLocal="90" where mac_address="00:06:5A:03:2E:5B"这样的更新查询.

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

推荐答案

在我的应用程序中,我使用以下方法.它根据给定的id找到一个对象,并通过Gson对其进行解析并将其保存到相应的对象中.

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

这篇关于如何在文档的子数组中使用mongodb java使用mongoDB查找文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆