Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

java - writing mongoDB syntax

hey guys how to write this mongoDB syntax to java

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

my source is

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

any solution guys? i am newbie on using mongoDB

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
    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);

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...