java中的mongodb嵌套搜索查询
我对MongoDB搜索查询有疑问。我有两个参数,我必须搜索“文本”或“用户名”,还有更多的参数 因此,SQL查询如下所示:java中的mongodb嵌套搜索查询,java,mongodb,find,mongodb-query,mongodb-java,Java,Mongodb,Find,Mongodb Query,Mongodb Java,我对MongoDB搜索查询有疑问。我有两个参数,我必须搜索“文本”或“用户名”,还有更多的参数 因此,SQL查询如下所示: select * from tbl where place='pune' and (gender='male' or type='life') and (text='hi' or username='hi') 在MongoDB会怎么样 现在,我可以只使用一个参数进行搜索: DBObject query = new BasicDBObject();
select * from tbl where place='pune' and (gender='male' or type='life') and (text='hi' or username='hi')
在MongoDB会怎么样
现在,我可以只使用一个参数进行搜索:
DBObject query = new BasicDBObject();
BasicDBList dbl = new BasicDBList();
if (cmodel.getGender() != null) {
if ("male".equals(cmodel.getGender().toLowerCase())) {
dbl.add(new BasicDBObject(CrushModel.COLUMN_GENDER, "male"));
} else if ("female".equals(cmodel.getGender().toLowerCase())) {
dbl.add(new BasicDBObject(CrushModel.COLUMN_GENDER, "female"));
} else {
dbl.add(new BasicDBObject(CrushModel.COLUMN_TYPE, "crush"));
}
dbl.add(new BasicDBObject(CrushModel.COLUMN_TYPE, "life"));
query.put("$or", dbl);
}
query.put("text", new BasicDBObject("$regex", String.format(".*((?i)%s).*", searchText)));
query.put(CrushModel.COLUMN_PLACE, new ObjectId(cmodel.getPlace().toString()));
DBCursor cursor = collection.find(query);
db.test.find( { $and : [ {gender : 'male' },
{ place :'pune'},{$or:[{text:/abc0/},{userName:/abc0/}, {type : 'life' }]}] } )
这不是我想要的方式
there is one more field in table name "type" ,
for ex. if gender is male/female for which type is "normal" then i also want to show the type "life" here
It is only showing me the either or result like the entries of all gender:male or only the entries of type "life", I want to show both of them.
它起作用了
db.test.find( { $and : [ { place : 'pune'},{$or:[{text:/hi u r looking awesome 10/},{userName:/hi u r looking awesome 10/}]},{$or:[{gender : 'male' },{type:'life'}]}] } )
谢谢@Disposermongo查询准确地说:
db.test.find( { $and : [ { gender : 'male' }, { place : 'pune' }, { $or : [ { text : 'hi' }, { username : 'hi' } ] } ] } )
java将是:
ArrayList orList = new ArrayList();
ArrayList andList = new ArrayList();
orList.add(new BasicDBObject("text", "hi"));
orList.add(new BasicDBObject("username", "hi"));
andList.add(new BasicDBObject("gender", "male"));
andList.add(new BasicDBObject("place", "pune"));
andList.add(new BasicDBObject("$and", orList));
BasicDBObject query = new BasicDBObject("$or", andList);
使用MongoDB Java驱动程序v3.2.2实现上述操作的快捷方式。您可以这样做:
FindIterable<Document> iterable = collection.find(Document.parse("{$and: [{gender: 'male'}, {place: 'pune' }, {$or: [{text: 'hi'}, {username: 'hi'}]}]}"));
findierable iterable=collection.find(Document.parse({$and:[{{gender:'male'},{place:'pune'},{$or:[{text:'hi'},{username:'hi'}]}]);
这将返回与上面相同的结果。感谢您的回复。我想在查询中再添加一个参数,即type,例如。如果性别为男性/女性,类型为“normal”,那么我还想在这里显示类型“life”。我尝试用这种方式进行查询,但这并没有给出确切的结果。find({$and:[{gender:'male'},{place:'pune'},{$or:[{text:/abc0/},{userName:/abc0/},{type:'life'}]})。请帮助。它现在可以工作了。谢谢:)db.test.find({$and:[{place:'pune'},{$or:[{text:/hi-u-r looking awesome 10/},{userName:/hi-u-r looking awesome 10/},{$or:[{gender:'male'},{type:'life'}]})