编写mongoDB语法

时间:2016-06-30 06:59:55

标签: java mongodb syntax

嘿伙计们如何将这个mongoDB语法写入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的新手

1 个答案:

答案 0 :(得分:1)

    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);

如果你想要'或者'条件,只需更换$和$或。 请注意,上述代码未经过正确测试,您可能需要对其进行一些修改才能使其正常工作。

你的问题不明确,但我希望我帮助过。