这真的很难。我想找到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"
等更新查询。
答案 0 :(得分:0)
在我的应用程序中,我使用以下方法。它根据给定的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;
}