我在mongo DB中有数据。但我是mongo的新手。有些人可以帮我从mongo获得正确的记录。
{
"_id" : ObjectId("5872edaf9704dc26c0cb770e"),
"type" : "PUSH_NOTIFICATION",
"alerts" : [
{
"_id" : ObjectId("5872edab9704dc26c0cb770c"),
"alertId" : NumberLong(0),
"userId" : NumberLong(1000100),
"alertCode" : 3,
"ticketId" : NumberLong(0),
}
],
"registrationId" : "010080fab2695a984fc16737b4637b40d4bbcfbfcfb87752851c99fd925e8427",
"deleted" : false
}
上面是一排mongo表。我想获取ticketId不等于0的记录.taminId在Alert对象里面,这是一个警报数组。 我想用java编写查询。
答案 0 :(得分:0)
你可以试试这个:
BasicDBObject Query = new BasicDBObject();
BasicDBObject fields = new BasicDBObject();
Query.put("alerts.ticketId",0);
fields.put("alerts.$", 1);
DBCursor cursor = collection.find(Query, fields);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
希望这有帮助!
答案 1 :(得分:0)
你可以这样做:
Query query = new Query();
query.addCriteria(Criteria.where("alerts.ticketId").not().is(0));
//OR
query.addCriteria(Criteria.where("alerts.ticketId").ne(0));
//OR using less than(lt) and greater than(gt) operator
query.addCriteria(Criteria.where("alerts.ticketId").lt(0).gt(0));