gpt4 book ai didi

java - 如何查找符合多个条件的文档

转载 作者:IT老高 更新时间:2023-10-28 13:35:28 25 4
gpt4 key购买 nike

我正在尝试使用操作数 AND 一起查询集合。我的 shell 版本正在运行:

db.widgets.find({color: 'black, shape: 'round', weight: 100})

我找不到 Java 等效项(使用 native driver )。我尝试了各种方法,但这是我最近的尝试:

// Find all black, round widgets with weight 100
List<BasicDBObject> criteria = new ArrayList<BasicDBObject>();
criteria.add(new BasicDBObject("color", "black"));
criteria.add(new BasicDBObject("shape", "round"));
criteria.add(new BasicDBObject("weight", 100));

DBCursor cur = widgets.find(new BasicDBObject("$and", criteria));

// Get all matching widgets and put them into a list
List<Widget> widgetList = new ArrayList<Widget>();
DBCursor cur = widgets.find(andQuery);
while (cur.hasNext()) {
widgetList.add(new Widget(cur.next()));
}

if (widgetList.isEmpty())
System.out.println("No results found");

有什么想法吗?

最佳答案

BasicDBObject criteria = new BasicDBObject();
criteria.append("color", "black");
criteria.append("shape", "round");
criteria.append("weight", 100);

DBCursor cur = widgets.find(criteria);

关于java - 如何查找符合多个条件的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24353580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com