编写 mongoDB 语法 [英] writing mongoDB syntax

查看:36
本文介绍了编写 mongoDB 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们如何将这个 mongoDB 语法写入 java

hey guys how to write this mongoDB syntax to java

db.users.find( { user_id: /bc/ },{user_name:/bc/},{age:/2/} )

我的来源是

BasicDBObject sortOrder = new BasicDBObject();
MongoClient mongoClient;
DB db;

DBCollection table;
DBCursor cursor = null;

mongoClient = new MongoClient("localhost", 27017);
db = mongoClient.getDB("stackoverflow");
boolean auth = db.authenticate("aku","kamu".toCharArray());
table = db.getCollection("questions");
cursor = table.find();

while (cursor.hasNext()) {
    DBObject object = cursor.next();
    out.print(object.get("title"));
    answer = rn.nextInt(8) + 0;
}

有什么解决办法吗?我是使用 mongoDB 的新手

any solution guys? i am newbie on using mongoDB

推荐答案

    DBObject idMatch = new BasicDBObject("user_id","bc");
    DBObject usernameMatch = new BasicDBObject("user_name",bc);
    DBObject ageMatch = new BasicDBObject("age",2);

    DBObject andAll = new BasicDBObject("$and", Arrays.asList(existence, firstNotMatch, secondNotMatch));

    //calling  table.find will get you what you wanted.
    table.find(andAll);

如果您想对条件进行或"运算,只需将 $and 替换为 $or.请注意,上面的代码没有经过正确测试,您可能需要对其进行一些修改才能使其正常工作.

If you want to 'OR' the conditions, just replace $and with $or. note that the above code is not tested properly and you may need to modify it a bit to make it work.

你的问题不清楚,但我希望我有所帮助.

Your question is not clear, but i hope i helped.

这篇关于编写 mongoDB 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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